zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    mysql find_int_set
    PHPSTROM8.0 注册码(7.1也可用)
    gym 101657 D
    gym101657 C
    poj 3525
    poj1279
    poj3335
    poj 1228
    poj 1873
    poj 2074
  • 原文地址:https://www.cnblogs.com/sishuiliuyun/p/3243587.html
Copyright © 2011-2022 走看看