简体   繁体   English

Android,从“最近的应用”中删除应用时取消通知

[英]Android, cancel notification when app is removed from “Recent apps”

In an android app, I create a notification when an app is moved to the background. 在Android应用程序中,我将应用程序移动到后台时创建通知。 When the notification is clicked, it moves the application back to the foreground. 单击通知后,它会将应用程序移回前台。

The problem I have is in the background, and the user removes the app through "Recent apps" (ie, he swipes it away), the notification stays. 我遇到的问题是在后台,用户通过“最近的应用程序”删除应用程序(即,他将其刷掉),通知保持不变。 How can I capture the swipe event, ie when the app is removed from "Recent apps". 如何捕获滑动事件,即从“最近的应用”中删除应用时。 I tried onDestroy, but that is not getting triggered. 我试过onDestroy,但是没有被触发。

Added in API level 14 , there is a callback method onTaskRemoved . API级别14中添加了一个onTaskRemoved回调方法 This is called if the service is currently running and the user has removed a task that comes from the service's application. 如果服务当前正在运行且用户已从服务的应用程序中删除了任务,则会调用此方法。

So in your Service class, who is posting the notification, you can override this method and stop the running service which is responsible for showing the notification. 因此,在发布通知的Service类中,您可以覆盖此方法并停止负责显示通知的正在运行的服务。

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        stopSelf();
    }

With above code inside your Service call, as soon as user removes the app through "Recent apps" (ie, he/she swipes it away), the onTaskRemoved callback for this service will be invoked and with stopSelf() app is stopping the service responsible for showing/posting the notification. 使用Service调用中的上述代码,只要用户通过“最近的应用程序”删除应用程序(即,他/她onTaskRemoved它),就会调用此服务的onTaskRemoved回调,并且使用stopSelf()应用程序停止服务负责显示/发布通知。

Consequently the posted notification will be removed by the system. 因此,系统将删除发布的通知。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android-从最新应用中删除的应用取消了前台通知 - android - foreground notification is cancelled on app removed from recent apps 从最近的任务中删除应用程序时,Android 通知被取消 - Android notification gets cancelled when app is removed from recent tasks android中的GCM,当从“设备最近的应用”部分中删除该应用实例时,未显示预期的活动 - GCM in android, not showing the intended activity when the app instance is removed from the Devices RECENT Apps section 从 Android 12 中的最近应用程序中删除应用程序时,音乐播放器前台服务停止 - Music Player Foreground service Stops when App is removed from recent apps in Android 12 从最新应用程序中删除应用程序后,自定义静态广播接收器不起作用 - custom static broadcastreceiver not working when app is removed from recent apps 从最近的应用程序中删除应用程序时,“前台”服务将被终止 - “foreground” service is killed when App is removed from recent apps 从最近的应用程序中删除应用程序后,IntentService停止工作 - IntentService stops working when app is removed from recent apps 从最近的应用程序中删除后,请不要杀死应用程序 - Don't kill app when removed from recent apps 从最近(内存)列表中删除应用程序时取消通知 - Cancel notification when app is removed from recents(memory) list 我想在应用被最近的应用杀死时显示通知 - I want to show notification when app killed from recent apps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM