如何当一个Android应用(活动或服务)被杀害删除所有通知? [英] How to remove all notifications when an android app (activity or service) is killed?

查看:1116
本文介绍了如何当一个Android应用(活动或服务)被杀害删除所有通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
<一href=\"http://developer.android.com/reference/android/app/Service.html#onTaskRemoved%28android.content.Intent%29\"相对=nofollow>公共无效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.

您可以删除您的应用程序已与 <创建的所有通知code> 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.

这篇关于如何当一个Android应用(活动或服务)被杀害删除所有通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆