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

bash - How do you programmatically add a bats test? -

Unable to open serial port using Putty to bluetooth device on windows 7 64 bit -

java - Getting exception in JDBC thin driver -