Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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" (i.e., he swipes it away), the notification stays. How can I capture the swipe event, i.e. when the app is removed from "Recent apps". I tried onDestroy, but that is not getting triggered.

like image 601
alexb Avatar asked Feb 24 '14 19:02

alexb


People also ask

Should we remove apps from recent?

You don't need to clear your apps on newer phone. Android will manage it memory. If you clear your apps to often, it will only slow down your phone and make it work harder thus run out of battery faster.

How do I clear notifications when an app is closed android?

To remove a persistent notification on Android as fast as possible, first, press-and-hold on it. Alternatively, swipe the notification left or right to reveal a gear icon on either side, and then tap on it. The notification expands. Tap on “Turn off notifications” at the bottom.

How do you dismiss a notification on android?

To dismiss a notification, touch it and swipe left or right. Tap the dismiss icon to dismiss all notifications. On newer versions of Android, you can manage some notifications from the lock screen. Double-tap a notification to open the app or swipe left or right to dismiss the notification.


1 Answers

Added in API level 14, there is a callback method 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.

    @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" (i.e., 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.

Consequently the posted notification will be removed by the system.

like image 89
AADProgramming Avatar answered Oct 21 '22 04:10

AADProgramming