I want to reset a variable of my service when user clears my notification: that’s all!
Looking around I see that everyone suggest to add a delete intent on my notification, but intent is used to start an activity, a service o whatever while I just need a thing like this:
void onClearPressed(){
aVariable = 0;
}
how to obtain this result?
Notifications are not managed by your app and all things like showing notifications and clearing them are actually happening in another process. You can’t make another app directly execute a piece of code just because of security reasons.
The only possibility in your case is to provide a
PendingIntentwhich just wraps around a regular Intent and will be started on behalf of your app when notification is cleared.You need to use
PendingIntentfor sending broadcast or starting a service and then doing what you want in the broadcast receiver or in the service. What exactly to use depends on from which application component you are showing notifications.In case of broadcast receiver you can just create an anonymous inner class for broadcast receiver and register it dynamically before showing notification. It will look something like that: