I have a problem with Android service restart. I am building against API version 7 and running on a device with Android 2.3.3.
The problem is, that when my service is killed by the system and is later restarted, only the onCreate()
of my service is called. code in onStartCommand()
is not executed. If I start my application for the first time, the code in onStartCommand()
is normally executed and all works fine, until system kills my service, then the service will not restart correctly.
Documentation says that onStartCommand()
is always called when a service is restarted. In the case of service restart, the onStartCommand()
should be called with null intent. This is not my case.
Any idea why this could happen?
You might be seeing a bug in Gingerbread. There was a thread in the android-developers group about it: onStartCommand bug thread
See the post numbered 26 by Dianne Hackborn for details.
This is a bug in Android 2.3 (API level 9), which got fixed in API level 10.
From Dianne Hackborns post regarding this issue:
Note that the git commit link previously did not work. Click here for a working mirror link to the commit and line that is causing the bug.
Also she meant 2.3, but wrote 3.3. She later corrected it and provided a suggestion for a workaround:
She also wrote that this behavior will be fixed in the next platform update (post-3.0):
I assume the API level she meant was 10, and I can confirm that
onStartCommand()
is called with null intents after a service restart when you switch from API level 9 to 10.