Cancel notification on remove application from multitask panel

前端 未结 7 1743
北恋
北恋 2020-11-27 14:15

I manage an ONGOING notification from my application (not from a service).

When I kill application from task manager with \"End\" button, no

7条回答
  •  無奈伤痛
    2020-11-27 15:10

    I would suggest You to use Deepti solution (https://stackoverflow.com/a/28584405/619673) which was

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel(NOTIFICATION_ID);
    }
    

    However, he forget mention one thing. If Your connection to service was by call bindService(), onTaskRemoved() won't be trigged. To handle this You must connect with service by call startService() as in example below:

    startService(new Intent(context, CustomerService.class));
    

    Once You call it, You will be able catch onTaskRemoved().

    Nothing stands in the way to call both methods startService() and somewhere further bindService().


    I was basing on solution Service: onTaskRemoved not called if started with bindService

    startService(new Intent(context, CustomerService.class)); // this will allow catch onTaskRemoved
    // ...
    bindService(new Intent(context, CustomerService.class),
                            mConnection, Context.BIND_AUTO_CREATE);
    

提交回复
热议问题