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

posted @ 2013-08-07 17:30  似水流云  阅读(309)  评论(0编辑  收藏  举报