Reputation: 11
Is there any way that when I click on the notification and then this notification can stop the service? And my notification is in my service.
Every time when I click on my notification, the activity will run onResume() method. I cannot stop this in this onResume() since I have another notification.
My app process is: a notification helps to tell the user it is now counting down the time, and when time is up, I will begin another notification to tell the user your time is up. So, I cannot stopservice before the "timeup" notification. My thought is, When the user click on "timeup" notification, then I can stop the service.
Can I do that?
Upvotes: 0
Views: 1548
Reputation: 4676
The lifecycle of an application puts OnResume at the start of every launch, so the best method would be to assign a new request code to each new notification and give the one you wish to recognize an action. If the action is received, you can stop the service. If it is not, then the app will be able to differentiate that it is a different notification and respond accordingly.
...
Intent intent = new Intent(this, TARGET.class);
intent.setAction("CUSTOM_ACTION");
PendingIntent pIntent = PendingIntent.getService(this,
(int) System.currentTimeMillis(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
...
@Override
public void onResume(){
super.onResume();
if (getIntent().getAction().equals("CUSTOM_ACTION")) {
// kill service
} else {
// other stuff
}
}
Upvotes: 1