开发者

How to restart an android service when it is interrupted by reboot?

开发者 https://www.devze.com 2023-04-12 07:24 出处:网络
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

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....

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号