How to restart an android service when it is interrupted by reboot?
I start an android service manually which does some data collection work.
Suppose if there is interruption caused by reboot then the service ends there.I need to restart the s开发者_C百科ame service on reboot.
note:I am not starting the service on boot. Thanks in advance.
You can restart service after boot interruption.
Just store flag in shared pref which is boolean set to true if boot interrupted.
Now when boot completes you can use the following code to restart your service.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyStartupIntentReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// check for interreupted flag in shared pref
// if true restart your service
}
}
Add following snippet to your manifest
<receiver android:name="MyStartupIntentReceiver">
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
Cheers....
精彩评论