Android Service is Quiting in Android 2.3.3

Ask by : mboy November 04, 2012 14:06

I have Android service to run and send email every 9 mins but after some cycles it quits. this app is also installed in my Android froyo but it works good for more than 2months now.. I can see the logs in logcat that the service quits but my problem is I cannot understand what does it mean.. Can someone help me with this? Thanks!! any help would be appreciated...

here is my source code:

Logcat logs:

View original question

Answer by : CommonsWareNovember 04, 2012 14:06

The point behind using AlarmManager is so that your service only needs to be in memory when it is doing actual work, and can go away in between AlarmManager events. You have managed to not do that, and therefore your code will not work reliably.

If you want to "send email every 9 mins", you should:

  1. Move the BroadcastReceiver to be a public Java class, registered in the manifest via a <receiver> element, and remove the registerReceiver()/unregisterReceiver() stuff.

  2. Switch your service to be an IntentService, so you get a background thread (which you need for your work, but your current code lacks) and so the service can automatically shut down when there is no more work to be done.

  3. Add in the logic for the WakeLock that you will need since you are using a _WAKEUP-style alarm. You could combine this and the previous steps by switching to my WakefulIntentService, if you wish.

  4. Deal with the case where the user reboots the device, if you want your alarms to continue after the reboot, such as by scheduling them again via an ACTION_BOOT_COMPLETED BroadcastReceiver.

View original answer