5

I have a foreground service to download some contents form the web.

Unfortunately, due to bug which reported here my service being killed when a receiver received a broadcast within my service but it's notification won't be killed and I saw the following log in my logcat:

I/ActivityManager(449): Killing 11073:my-package-name/u0a102 (adj 0): remove task

Is there anyway to to destroy foreground notification when it's parent service get killed by OS?

Community
  • 1
  • 1
Ali
  • 2,012
  • 3
  • 25
  • 41

6 Answers6

1

Service ending remove the foreground notification.

@Override
public void onDestroy() {
    // mycleanup first
    stopForeground(true);
    super.onDestroy();
}
danny117
  • 5,581
  • 1
  • 26
  • 35
1

Use stopForeground:

@Override
public void onDestroy() {
 // as the bug seems to exists for android 4.4
 if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.KITKAT)
 {
    stopForeground(true);
 }
  super.onDestroy();
}

or

public void onTaskRemoved (Intent rootIntent)
{
 // as the bug seems to exists for android 4.4
 if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.KITKAT)
 {
   stopForeground(true);
 }
}
Sagar Pilkhwal
  • 3,998
  • 2
  • 25
  • 77
1

Inside the notification check periodically (e.g. Handler) if service is still running using code below:

public static boolean isMyServiceRunning(Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))                           {
        if (MyService.class.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

And if not just shut down notification.

private static final int DEALY = 10000;
private Handler handler = new Handler();

...
handler.postDelayed(ensureSericeIsRunning, DELAY);
...

private Runnable ensureSericeIsRunning = new Runnable() {

    @Override
    public void run() {
        if (!isMyServiceRunning(getActivity())){
            //shut down notification
        } else {
            handler.postDelayed(ensureSericeIsRunning, DELAY);
        }
    }
};
bgplaya
  • 1,105
  • 15
  • 27
  • Great suggestion but please note that whenever service get killed by OS all other references will be removed too! – Ali Sep 15 '14 at 23:26
  • What do you mean? Doesn't notification still available to cancel with a Notification Manager? – bgplaya Sep 16 '14 at 05:35
  • I believe it is what you need: while serivce will be killed next call of isMyServiceRunning will return false and you can just cancel notification. Could you clarify what's wrong? – bgplaya Sep 16 '14 at 05:43
0

You could remove the notification from onStop or onDestroy inside of your service class. P.S there no guarantee this would work, in api 14 and above the service can still be running even without foreground notification, you could return START_STICKY that will bring back your service even if the system destroyed it, system will restart it.

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}
Kosh
  • 6,140
  • 3
  • 36
  • 67
  • Nope it doesn't work in my case, even `START_STICKY` does not restart the service in KitKat!!! – Ali Sep 09 '14 at 11:38
  • Can u share which phone u using and are u using stock rom or custom? – Kosh Sep 09 '14 at 23:32
  • Yes, It's Nexus 5 with AOSP KitKat ROM, please also check this link http://stackoverflow.com/questions/20636330/start-sticky-does-not-work-on-android-kitkat-edit-and-jelly-bean/20681898#20681898 – Ali Sep 10 '14 at 01:13
  • that bug im not facing using Galaxy s5 rooted stock rom, two days ago i was using CM11, and trust me developing on AOSP isn't a good idea, i had a lots of issues regards my last freelancing app, while running on CM11, try to revert to stock rom, and test it out. you will see loads of differences between running on stock and AOSP. – Kosh Sep 10 '14 at 02:07
0

use onDestroy(). Even tho the documentation says it is not guaranteed to be called, I have tested multiple times and the only cases it doesn't get called is when the system kills the process, but to that point your notification will be killed to, so it's ok.

Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
0

use the service onTaskRemoved() method.

jinnybrain
  • 30
  • 1
  • 2