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

前端 未结 5 1095
情书的邮戳
情书的邮戳 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:20

    @See public void onTaskRemoved(Intent rootIntent). This is called if the service is currently running and the user has removed a task that comes from the service's application. If you have set ServiceInfo.FLAG_STOP_WITH_TASK flag then you will not receive this callback; instead, the service will simply be stopped.

    You can remove all notifications that your app has created with cancelAll():

    NotificationManager nManager = ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
    nManager.cancelAll();
    

    According to the API docs, this will

    Cancel all previously shown notifications.

    in the same way that cancel(int id) does:

    Cancel a previously shown notification. If it's transient, the view will be hidden. If it's persistent, it will be removed from the status bar.

提交回复
热议问题