Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all notifications when an android app (activity or service) is killed?

I have an app with an activity and an intent service running in the background.

There's also a notification which shows a progress bar with the progress of the background process (it's downloading a lot of files from a server).

The activity shows the download progress ( user can't meaningfully use the app before these assets are downloaded). When this activity is closed, the notification with the progress bar is shown.

My issue: When the application is killed via the "task manager" (accessible via right button on android 4.0, the one with the two rectangles), the notification is still there. But it's basically a zombie, because the service that used to update it is dead.

Put in a different way: How can I remove a (or all) notification(s) when my application is killed?

like image 603
treesAreEverywhere Avatar asked Mar 05 '14 21:03

treesAreEverywhere


People also ask

How do I clear my ongoing notifications on Android?

First, press-and-hold on the persistent notification you want to remove. Another option is to swipe the notification left or right, and then tap on the cogwheel icon shown next to it. Next, tap on the switch next to Permanent to disable it, and then press Save.

Which method is called when app is killed Android?

When Android decides to kill our app, our activities will call onDestroy method.

How do you check app is killed or not in Android?

there's no way to determine when a process is killed. From How to detect if android app is force stopped or uninstalled? When a user or the system force stops your application, the entire process is simply killed. There is no callback made to inform you that this has happened.


1 Answers

@See public void onTaskRemoved(Intent rootIntent). This is called if the service is currently running and the user has removed a task that comes from the service's application. If you have set ServiceInfo.FLAG_STOP_WITH_TASK flag then you will not receive this callback; instead, the service will simply be stopped.

You can remove all notifications that your app has created with cancelAll():

NotificationManager nManager = ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
nManager.cancelAll();

According to the API docs, this will

Cancel all previously shown notifications.

in the same way that cancel(int id) does:

Cancel a previously shown notification. If it's transient, the view will be hidden. If it's persistent, it will be removed from the status bar.

like image 171
AZ_ Avatar answered Sep 27 '22 16:09

AZ_