android 两个service实现相互保活

1,实现原理,2个service不在同一个进程中,一个A进程,一个B进程,A和B通过广播机制来通讯,当A进程被杀死的时候,发送广播给B进程,B进程收到广播后再次启动A服务,相反的B进程被杀死,由A来重新启动。

2,A 服务的源码如下:

public class ServiceOne extends Service {

    String TAG="ServiceOne";
    @Override
    public void onCreate() {
        super.onCreate();
        registerBroadcast();
        LogUtil.d(TAG,"ServiceOne onCreate");
    }
    void registerBroadcast(){
        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction("com.foxconn.androidlib.ServiceTwoKilled");
        registerReceiver(mReceiver, mFilter);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        Intent intent = new Intent();
        intent.setAction("com.foxconn.androidlib.ServiceOneKilled");
        sendBroadcast(intent);
        unregisterReceiver(mReceiver);
        LogUtil.d(TAG,"ServiceOne onDestroy");
        super.onDestroy();
    }


    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            LogUtil.d(TAG,"ServiceOne onReceive 执行");
            Intent service = new Intent("com.foxconn.androidlib.brodcast.ServiceTwo");
            String packageName = "com.foxconn.androidlib";//需要开启服务的app包名
            service.setComponent(new ComponentName(packageName, "com.foxconn.androidlib.brodcast.ServiceTwo"));
            startService(service);
        }
    };
}

 

3,B服务的源码如下:

public class ServiceTwo extends Service {
    String TAG="ServiceTwo";
    @Override
    public void onCreate() {
        super.onCreate();
        registerBroadcast();
        LogUtil.d(TAG,"ServiceTwo onCreate");
        count();

    }

    void count(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    LogUtil.d(TAG,"一直打印"+new Random().nextLong());
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

    }

    void registerBroadcast(){
        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction("com.foxconn.androidlib.ServiceOneKilled");
        registerReceiver(mReceiver, mFilter);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        Intent intent = new Intent();
        intent.setAction("com.foxconn.androidlib.ServiceTwoKilled");
        sendBroadcast(intent);
        unregisterReceiver(mReceiver);
        LogUtil.d(TAG,"ServiceTwo onDestroy");
        super.onDestroy();
    }


    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            LogUtil.d(TAG,"ServiceTwo onReceive 执行");
            Intent service = new Intent("com.foxconn.androidlib.brodcast.ServiceOne");
            String packageName = "com.foxconn.androidlib";//需要开启服务的app包名
            service.setComponent(new ComponentName(packageName, "com.foxconn.androidlib.brodcast.ServiceOne"));
            startService(service);
        }
    };
}

4,AndroidManifest.xml 代码如下:

        
        
        
        

 

你可能感兴趣的:(Android)