进程保活之双进程守护

双进程守护场景:home键,系统应用管理,直接杀死进程,进程仍然处于运行状态;
适用手机类型:50%的手机。
双进程守护原理:
进程A 进程B
删除A,同时创建B
删除B,同时创建A
前提相关知识:
1、Service分类:一种localService 也就是普通的Service;另一种是RemoteService,远程服务,也就是我们常说的AIDL,它是由IPC引进的一种链接两个进程,两个app的技术;
2、AIDL使用模式:
①定义AIDL接口;
②为远程服务实现对应的stub;
③将服务暴露给客户程序;
代码实现:
一、创建IServiceAidlInterface.aidl

// Declare any non-default types here with import statements

interface IServiceAidlInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
}

二、创建本地LocalService

public class LocalService extends Service {
    private static String TAG = "LocalService";
    LocalServiceBinder localBinder;
    LocalServiceConnection localCnn;
    private int count = 0;


    @Override
    public void onCreate() {
        super.onCreate();
        if (localBinder == null) {
            localBinder = new LocalServiceBinder();
        }
        localCnn = new LocalServiceConnection();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startTime();
        Notification.Builder builder = new Notification.Builder(this);
        builder.setDefaults(Notification.DEFAULT_SOUND);
        builder.setContentTitle("Xiaoke Wang");
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentInfo("info");
        builder.setWhen(System.currentTimeMillis());
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
        builder.setContentIntent(pi);
        //将serviceprocess提高到foreground process 优先级
        startForeground(startId, builder.build());
        //确保后台运行
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
//        throw new UnsupportedOperationException("Not yet implemented");
        return localBinder;
    }

    /**
     * 暴露外部接口
     */
    class LocalServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

        }

        /**
         * 当服务断开连接时调用,本地服务断开时创建另一个服务
         * @param componentName
         */
        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.d(TAG, "onServiceDisconnected");
            LocalService.this.startService(new Intent(LocalService.this, RemoteService.class));
            LocalService.this.bindService(new Intent(LocalService.this, RemoteService.class), localCnn, Context.BIND_IMPORTANT);
        }
    }

    /**
     * 为远程服务实现对应的stub
     */
    class LocalServiceBinder extends IServiceAidlInterface.Stub {

        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
            Log.d(TAG, "basicTypes");
        }
    }

三、创建RemoteService:

public class RemoteService extends Service {
    private static final String TAG = "RemoteService";
    RemoteServiceBinder remoteBinder;
    RemoteServiceConnection remoteCnn;
    private int count=0;


    @Override
    public void onCreate() {
        super.onCreate();
        if (remoteBinder ==null){
            remoteBinder =new RemoteServiceBinder();
        }
        remoteCnn =new RemoteServiceConnection();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
//        throw new UnsupportedOperationException("Not yet implemented");
        return remoteBinder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startTime();
        Notification.Builder builder=new Notification.Builder(this);
        builder.setDefaults(Notification.DEFAULT_SOUND);
        builder.setContentTitle("Xiaoke Wang");
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentInfo("info");
        builder.setWhen(System.currentTimeMillis());
        PendingIntent pi=PendingIntent.getActivity(this,0,intent,0);
        builder.setContentIntent(pi);
        //将serviceprocess提高到foreground process 优先级
        startForeground(startId,builder.build());
        return START_STICKY;
    }

    /**
     * 暴露外部接口
     */
    class RemoteServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.d(TAG,"onServiceDisconnected");
            RemoteService.this.startService(new Intent(RemoteService.this,LocalService.class));
            RemoteService.this.bindService(new Intent(RemoteService.this,LocalService.class),remoteCnn, Context.BIND_IMPORTANT);
        }
    }

    /**
     * 为远程服务实现对应的stub
     */
    class RemoteServiceBinder extends IServiceAidlInterface.Stub {


        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
            Log.d(TAG, "basicTypes");
        }
    }

四、清单文件配置:

   <service
            android:name=".LocalService"
            android:enabled="true"
            android:exported="true" />
        <service
            android:name=".RemoteService"
            android:enabled="true"
            android:exported="true"
            android:process=":remote"/>

五、MainAct开启服务:

  startService(new Intent(this,LocalService.class));
        startService(new Intent(this,RemoteService.class));

ok项目结束,相关源码,请留下邮箱。

你可能感兴趣的:(项目中小功能抽离)