Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8518455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:58:55+00:00 2026-06-11T05:58:55+00:00

I’ve been using Android open source service example. I just need to use it

  • 0

I’ve been using Android open source service example. I just need to use it to send notification to user, but strange, it allocates lots of memory. I checked in Running Services, and it is almost 20MB (if i set ACTION_BACKGROUND) or 30MB (if i set ACTION_FOREGROUND)…

What should i do to reduce this memory usage?

I’ve already read this discussion I have no bitmap or webview.

Here’s my service:

/**
* This is an example of implementing an application service that can
* run in the "foreground".  It shows how to code this to work well by using
* the improved Android 2.0 APIs when available and otherwise falling back
* to the original APIs.  Yes: you can take this exact code, compile it
* against the Android 2.0 SDK, and it will against everything down to
* Android 1.0.
*/

public class NotificationService extends Service {

static final String ACTION_FOREGROUND = "com.example.android.apis.FOREGROUND";
static final String ACTION_BACKGROUND = "com.example.android.apis.BACKGROUND";

private static final Class<?>[] mSetForegroundSignature = new Class[] {
    boolean.class};
private static final Class<?>[] mStartForegroundSignature = new Class[] {
    int.class, Notification.class};
private static final Class<?>[] mStopForegroundSignature = new Class[] {
    boolean.class};

//    protected NotificationManager mNM;

private Method mSetForeground;
private Method mStartForeground;
private Method mStopForeground;
private Object[] mSetForegroundArgs = new Object[1];
private Object[] mStartForegroundArgs = new Object[2];
private Object[] mStopForegroundArgs = new Object[1];

void invokeMethod(Method method, Object[] args) {
    try {
        method.invoke(this, args);
    } catch (InvocationTargetException e) {
        // Should not happen.
        Log.w("ApiDemos", "Unable to invoke method", e);
    } catch (IllegalAccessException e) {
        // Should not happen.
        Log.w("ApiDemos", "Unable to invoke method", e);
    }
}

/**
 * This is a wrapper around the new startForeground method, using the older
 * APIs if it is not available.
 */
void startForegroundCompat(int id, Notification notification) {
    // If we have the new startForeground API, then use it.
    if (mStartForeground != null) {
        mStartForegroundArgs[0] = Integer.valueOf(id);
        mStartForegroundArgs[1] = notification;
        invokeMethod(mStartForeground, mStartForegroundArgs);
        return;
    }

    // Fall back on the old API.
    mSetForegroundArgs[0] = Boolean.TRUE;
    invokeMethod(mSetForeground, mSetForegroundArgs);
    // mNM.notify(id, notification);
}

/**
 * This is a wrapper around the new stopForeground method, using the older
 * APIs if it is not available.
 */
void stopForegroundCompat(int id) {
    // If we have the new stopForeground API, then use it.
    if (mStopForeground != null) {
        mStopForegroundArgs[0] = Boolean.TRUE;
        invokeMethod(mStopForeground, mStopForegroundArgs);
        return;
    }

    // Fall back on the old API.  Note to cancel BEFORE changing the
    // foreground state, since we could be killed at that point.
    // mNM.cancel(id);
    mSetForegroundArgs[0] = Boolean.FALSE;
    invokeMethod(mSetForeground, mSetForegroundArgs);
}

@Override
public void onCreate() {
    // mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    try {
        mStartForeground = getClass().getMethod("startForeground",
                mStartForegroundSignature);
        mStopForeground = getClass().getMethod("stopForeground",
                mStopForegroundSignature);
        return;
    } catch (NoSuchMethodException e) {
        // Running on an older platform.
        mStartForeground = mStopForeground = null;
    }
    try {
        mSetForeground = getClass().getMethod("setForeground",
                mSetForegroundSignature);
    } catch (NoSuchMethodException e) {
        throw new IllegalStateException(
                "OS doesn't have Service.startForeground OR Service.setForeground!");
    }
}

@Override
public void onDestroy() {
    // Make sure our notification is gone.
    stopForegroundCompat(1);
}

// This is the old onStart method that will be called on the pre-2.0
// platform.  On 2.0 or later we override onStartCommand() so this
// method will not be called.
@Override
public void onStart(Intent intent, int startId) {
    handleCommand(intent);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    handleCommand(intent);
    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    return  START_STICKY;
}

@Override
public void onRebind(Intent intent) {
    super.onRebind(intent);
    handleCommand(intent);
}

void handleCommand(Intent intent) {
    if (intent == null)
        return;

    if (ACTION_FOREGROUND.equals(intent.getAction())) {

        DBHelper db = new DBHelper(this);

        String lastTime = db.getLastVisitTime();
        if(!lastTime.equals("-1")) {
            new Notifications(this).InviteUser();
        }

        String target = db.getTargetValue();
        if(target.equals("")) {
            new Notifications(this).TargetlessNotification();
        }

        db.close();

        /*
        // In this sample, we'll use the same text for the ticker and the expanded notification 
        CharSequence text = getString(R.string.app_name); 
        CharSequence description = getString(R.string.recall_user);

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.icon, text, System.currentTimeMillis());

        // The PendingIntent to launch our activity if the user selects this notification PendingIntent
        contentIntent = PendingIntent.getActivity(this, 1, new Intent(this, YKEYarinaSaklaActivity.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, text, description, contentIntent);

        // Set properties of notification 
        notification.flags = Notification.FLAG_INSISTENT | Notification.FLAG_AUTO_CANCEL; 
        notification.defaults |= Notification.DEFAULT_ALL;

        startForegroundCompat(1, notification);
        */


    } else if (ACTION_BACKGROUND.equals(intent.getAction())) {
        stopForegroundCompat(1);
    }
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}
}

P.S.: I don’t know if it’s relevant or not but i’m starting this service onDestroy of my app, so it’ll send notification to user on a specific time with AlarmManager. (So it should not be killed to avoid AlarmManager deleting my notification.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:58:56+00:00Added an answer on June 11, 2026 at 5:58 am

    I’ve tried to simplfy my service as possible as i can, but the situation is still the same… Then i realize that somehow, usage of memory decrease by itself… So, if i have no option, i could except that.

    public class NotificationService2 extends Service{
    
    private String target, lastTime, notifCheck, notifCheck2;
    
    @Override
    public void onStart(Intent intent, int startId) {
    
        Bundle extras = intent.getExtras();
        if(extras != null) {
            this.lastTime = extras.getString("lastTime");
            this.target = extras.getString("target");
            this.notifCheck = extras.getString("notifCheck");
            this.notifCheck2 = extras.getString("notifCheck2");
        }
    
        handleCommand(intent);
    
        super.onStart(intent, startId);
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    
        Bundle extras = intent.getExtras();
        if(extras != null) {
            this.lastTime = extras.getString("lastTime");
            this.target = extras.getString("target");
            this.notifCheck = extras.getString("notifCheck");
            this.notifCheck2 = extras.getString("notifCheck2");
        }
    
        handleCommand(intent);
    
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        handleCommand(intent);
        return null;
    }
    
    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
        handleCommand(intent);
    }
    
    void handleCommand(Intent intent) {
        if (intent == null)
            return;
    
        String lastTime = this.lastTime;
        String notifCheck = this.notifCheck;
        String target = this.target;
        String notifCheck2 = this.notifCheck2;
    
        if(lastTime != null && notifCheck != null) {
            if(!lastTime.equals("-1") && !notifCheck.equals("1")) 
                new Notifications(this).InviteUser();
        } else this.stopSelf();
    
        if(target != null && notifCheck2 != null) {
            if(target.equals("") && !notifCheck2.equals("1")) 
                new Notifications(this).TargetlessNotification();
        } else this.stopSelf();
    
    }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.