如何当一个Android应用(活动或服务)被杀害删除所有通知?被杀、通知、Android

2023-09-07 22:45:32 作者:小丑忧伤谁会懂i

我有一个活动,并在后台运行的意图服务的应用程序。

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 hows 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.

我的问题:当应用程序通过任务管理器(通过在Android 4.0,一个与两个矩形右边的按钮访问)被杀,通知仍然存在。但它基本上是一个僵尸,因为用来更新服务已经死了。

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.

放在一个不同的方式:我如何可以删除(或全部)的通知(S)时,我的应用程序被杀害

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

推荐答案

@see公共无效onTaskRemoved(意向rootIntent)。这是如果服务正在运行调用和用户已经删除了来自该服务的应用的任务。如果已设置 ServiceInfo.FLAG_STOP_WITH_TASK 标志,那么您将不会收到此回调;相反,该​​服务将简单地停止。

@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.

您可以删除您的应用程序已与 cancelAll() :

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

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

根据API文档,这将

According to the API docs, this will

取消所有previously显示通知。

Cancel all previously shown notifications.

以相同的方式, 取消(INT ID) 做:

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

取消previously显示通知。如果是短暂的,视图将被隐藏。如果它是持久的,它会从状态栏被删除。

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.