How to override on swipe from recents list in Andr

2019-08-28 16:44发布

问题:

I have been working on an Android app that has some basic toggles and a Drive Mode feature. In the Drive Mode, the user may optionally auto reply to incoming notifications and all this works fine. I even have a persistent notification in the status bar when Drive Mode is started. One problem is, however, when my app is swiped away from the recents menu in Android, Drive Mode is stopped but the notification is still there. So my question is, how can I either keep Drive Mode running, or how can I remove the notification.

I know this is probably frowned upon but instead of listing my code here, you may find it at

http://github.com/sociallymellow/mango

and if you would like to download the apk (or see my other projects) visit

http://mellowdev.net

I have attempted to make this a service but no luck. I have also attempted to @Override the onDestroy method with no luck. Thanks!

回答1:

I have hidden my app from the recents list to overall fix my problem by adding android:label="@string/app_name" to the manifest. I have also added the following as suggested in a comment.

@Override
public void onDestroy() {
    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    nm.cancelAll();
}

Thank you all for the help and using both of these together completely solved my problem. I hope this helps others as well!