从最近(内存)列表中删除应用程序时取消通知 [英] Cancel notification when app is removed from recents(memory) list

查看:81
本文介绍了从最近(内存)列表中删除应用程序时取消通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用显示持续通知的服务.当应用程序正常关闭时,通知消失.但是,如果用户从最近的列表中删除它,应用程序将关闭并停止该服务.但是,通知仍然出现.这不是一个好的用户体验.

I am using a service which shows an ongoing notification. when the app gets closed normally, notification disappears. But, if the user removed it from recent list app is closing and stops the service. But, the notification still appears. It is not a good user experience.

注意:我尝试使用 onTaskRemoved() 但它根本没有被调用.

Note: I tried with onTaskRemoved() but it's not at all called.

当应用从最近列表中删除时,如何删除通知.

How can I remove notification when app removed from recent list.

更新:我注意到当从最近的应用程序中删除应用程序或从其他应用程序中删除应用程序时,会调用以下 3 行.

Update: I have noticed the following 3 lines are called when the app is removed from recents or killed from other apps.

11-27 11:44:05.097 3916-4157/? D/StatusBar: onNotificationRemoved:  Key: 
0|com.example.player|888|null|10525


11-27 11:44:05.107 3916-3916/? D/PhoneStatusBar: removeNotification key=android.os.Binder@2e604ccc keyCode=778063052 old=StatusBarNotification(pkg=com.example.player user=UserHandle{0} id=888 tag=null score=0 key=0|com.example.player|888|null|10525: Notification(pri=0 contentView=com.example.player/0x109008a vibrate=null sound=null defaults=0x0 flags=0x2 color=0xff40444a category=service actions=3 vis=PUBLIC))


11-27 11:44:05.137 4759-4759/? I/Launcher.ApplicationsMessage: update com.example.player/ to null

但是,在我的应用程序中,这 3 行在应用程序终止时调用,而不是从最近删除.我想这可能是一个问题(我遗漏了一些东西).

But, in my app, these 3 lines called when app killed not on removed from recents. I guess this may be a problem(I am missing something).

推荐答案

最后,我得到了解决方案.

Finally, I got solution.

我在构建通知时错过了启动前台服务.

I missed starting foreground service while building notification.

添加了这两行,当应用从最近列表中移除/被杀死时,通知会自动移除.

added these two lines and notification removed automatically when app removed from recent list / killed.

onBuildNotification():

onBuildNotification():

startForeground(NOTIFICATION_ID, notification);

onRemovenotification():

onRemovenotification():

 stopForeground(true);

这篇关于从最近(内存)列表中删除应用程序时取消通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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