Reputation: 2091
I am implementing some code and I want to run it in regular time(auto refreshing after some time...)I decided to go with alarm manager,now as I know that we can use pending intent for calling some another app to do some work on behalf of our app.Here using alarm manager I start one service like..
intent = new Intent(AutoUpdateActivity.this, AlarmService.class);
pI = PendingIntent.getService(AutoUpdateActivity.this,0, intent, 0);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime,10*1000,pI);
upto this everything is fine also my service runs after every 10 sec.Now when I close my app I wish to cancel alarmManager and I do it like this
pI.cancel();
this is also working fine,and my service does not run again..
But when I go to setting and check for running services it still shows me that service running.Where I am going wrong?is there anything that I am misleading ?
For my easy solution I stop my service like this
pI.cancel();
stopService(intent);
this cancel pI as well as stop service,but I am not sure is this correct way. so kindly share your knowledge and let me know where i am going wrong.
Upvotes: 0
Views: 160
Reputation: 28063
Maybe you can try to cancel the scheduled task too with:
alarmManager.cancel(pI);
Upvotes: 0
Reputation: 21909
The best way to do this is to call stopSelf()
inside your service when it has completed its work. This will effectively shut down the service when it is not needed.
Having services immediately shut down when they complete means you are very unlikely to get third-party TaskKiller apps killing things off when you don't wan or expect them to.
Upvotes: 2