How to remove all notifications when an android app (activity or service) is killed?

前端 未结 5 1097
情书的邮戳
情书的邮戳 2020-12-31 09:20

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 backg

5条回答
  •  一向
    一向 (楼主)
    2020-12-31 10:16

    Did you tried onStop ? It worked for me. I called cancelAll() in onStop. It closes all notification on removal of app from task Manager.

    Note : But it also remove all notification when your app goes in background. In that case you can re invoke those notification using background process.

    @Override public void onStop (){
        super.onStop();
        cancelAllNotification(getApplicationContext());
    }
    
    public static void cancelAllNotification(Context ctx) {
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
        nMgr.cancelAll();
    }
    

提交回复
热议问题