Can't Stop A Service Started With Alarmmanager
Solution 1:
You have to make the intent unique.
sth similar to:
locationPollerIntent.setData((Uri.parse("YOUR UNIQUE ID FOR THIS INTENT")));
AlarmManager will check the data field of an intent when getting back the right alarm for you. just set a unique Uri to the data of the intent.
so when you get back your alarm, do sth like:
intentToStop.setData((Uri.parse("YOUR SAME UNIQUE ID FOR THIS INTENT")));
the alarm should be cancelled after.
Solution 2:
REQUEST_CODE
needs to be the same in both places; you do not show where that is defined in your code listings.
Also, you do not need getApplicationContext()
here -- this
will work just fine.
Solution 3:
You have to pass the same instance.
publicstaticfinalUriMYURI= Uri.parse("Some_Uri");
[...]
locationPollerIntent.setData(MyClass.MYURI);
[...]
intentToStop.setData(MyClass.MYURI);
If you have two different references each time you create the Intent, it won't work.
More detailed example here:
http://malubu.wordpress.com/2012/06/05/take-your-time-widgets-and-alarmmanager/
Solution 4:
I had the same issue, its not about having the same instance of the Intent "locationPollerIntent" and "intentToStop" but you must have the same instance for the PendingIntent.
After stoping the AlarmManager, the service stops too.
static PendingIntent instance;
publicstatic PendingIntent getInstance(Context context) {
if (instance == null)
{
Intent intent = new Intent(context, LocationPoller.class);
instance = PendingIntent.getService(context, REQUEST_CODE, intent, 0);
}
return instance;
}
Post a Comment for "Can't Stop A Service Started With Alarmmanager"