Android——service重启

一、在application中注册消息监听

public class BackgroundServiceApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//该广播注册后可每分钟发送一次该广播,用来判断service的状态,是否进行重新启动。
//此action只能通过动态注册
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_TIME_TICK);
ServiceBroadcastReceiver receiver = new ServiceBroadcastReceiver();
registerReceiver(receiver, intentFilter);
}
}

注册消息监听,并重启服务

public class ServiceBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//重启service
if (action.equals(Intent.ACTION_TIME_TICK)) {
if (!BackgroundService.checkServiceStatus(context)) {
Intent service = new Intent(context, BackgroundService.class);
context.startService(service);
}
}
}
}

更多:http://blog.csdn.net/jayqean/article/details/7740120

上一篇:《java语言程序设计》初步学习——各种小Demo


下一篇:Django 日志配置