android - cancel Service when user clicks Notification -


similar this post got service work in background. when service starts, i'm displaying notification. can assing intent notification triggered when user clicks onto notification. activity started.

but how can use notification inform background service user want's cancel operation?

you can have broadcastreceiver stop service, triggered through pendingintent of notification.

have @ pendingintent.getbroadcast(...) , use instead of pendingintent.getactivity() method. can use this:

  intent broadcastintent = new intent(context, yourbroadcastreceiver.class);         pendingintent pendingintent = pendingintent.getbroadcast(context, 0, broadcastintent, 0);   notification noti = new notification.builder(this)          ...          .setcontent(pendingintent);          .build();    notification.setcontent(pendingintent); 

the broadcastreceiver inner class in service call stopservice() in onreceive() method.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -