进程保活之双进程守护

上一篇文章讲到一个像素保活线程。没有看的小伙伴可以去看看。地址:http://www.jianshu.com/p/a61ecc016aa5

原理:

杀进程是一个一个杀的。我们可以定义两个进程,两个进程相互监听。A进程被杀死,B进程监听到就启动A进程。否则相反。

实现:

1,创建MainActivity。一上来就启动两个服务。

package com.zsj.doubleprocess;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 一上来就启动两个服务。
        startService(new Intent(this, LocalService.class));
        startService(new Intent(this, RemoteService.class));
    }
}

2,创建两个进程的服务LocalService和RemoteService,并相互绑定。在连接断开的启动对方的进程的服务。

LocalService.java

package com.zsj.doubleprocess;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;

/**
 * 第一个服务。本地服务
 * 
 * @author zsj
 *
 */
public class LocalService extends Service {

    private MyBind bind;
    private MyServiceConnection conn;

    @Override
    public void onCreate() {
        super.onCreate();
        if (bind == null) {
            bind = new MyBind();
        }
        if (conn == null) {
            conn = new MyServiceConnection();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 绑定另外进程的服务RemoteService,并设置BIND_IMPORTANT为重要绑定
        bindService(new Intent(LocalService.this, RemoteService.class), conn,
                Context.BIND_IMPORTANT);

        return super.onStartCommand(intent, flags, startId);
    }

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

    public class MyBind extends RemoteConn.Stub {
        @Override
        public String getProcessName() throws RemoteException {
            return "LocalService";
        }
    }

    public class MyServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // 远程服务连接成功回调
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // 被绑定的服务断开连接的时候回调
            // 启动被绑定的服务。
            startService(new Intent(LocalService.this, RemoteService.class));
            bindService(new Intent(LocalService.this, RemoteService.class),
                    conn, Context.BIND_IMPORTANT);
        }
    }

}

RemoteService.java

package com.zsj.doubleprocess;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;

public class RemoteService extends Service {

    private MyBind bind;
    private MyServiceConnection conn;

    @Override
    public void onCreate() {
        super.onCreate();
        if (bind == null) {
            bind = new MyBind();
        }
        if (conn == null) {
            conn = new MyServiceConnection();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 绑定另外进程的服务LocalService,并设置BIND_IMPORTANT为重要绑定
        bindService(new Intent(RemoteService.this, LocalService.class), conn,
                Context.BIND_IMPORTANT);

        return super.onStartCommand(intent, flags, startId);
    }

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

    public class MyBind extends RemoteConn.Stub {
        @Override
        public String getProcessName() throws RemoteException {
            return "RemoteService";
        }
    }

    public class MyServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // 远程服务连接成功回调

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // 被绑定的服务断开连接的时候回调
            // 启动被绑定的服务。
            startService(new Intent(RemoteService.this, LocalService.class));
            bindService(new Intent(RemoteService.this, LocalService.class),
                    conn, Context.BIND_IMPORTANT);
        }
    }
}‘

3,因为两个服务在不同的进程里面.所以在AndroidManifest.xml配置:

图片发自简书App

4.在不同的进程通信。用到Aidl.定义一个通信接口RemoteConn.aidl。

RemoteConn.aidl

package com.zsj.doubleprocess;

interface RemoteConn {
    String getProcessName();
}

双进程守护就完成了。
因为这两个服务都是后台服务。为了提高更加不容易被杀死。可以提升为前台服务。就不给实现了。很简单。

优点:

可以防止360等第三方清理工具杀死。

弊端:

但是当用户手动点击强制停止。进程真的就停止了。

下次写就算用户手动点击强制停止。也能自动唤醒进程。

demo下载:
https://github.com/zsj1225/DoubleProcess

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,808评论 25 709
  • 什么是进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单...
    晨起清风阅读 1,837评论 0 5
  • 上篇我们讲解了Android中的5中等级的进程,分别是:前台进程、可见进程、服务进程、后台进程、空进程。系统会按照...
    徐爱卿阅读 3,898评论 6 33
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,045评论 19 139
  • 天猫的新Banner式样广告6秒后收入banner中 非弹层广告 干扰性低 流畅自然与界面融为一体 不影响操作
    小凡03004阅读 174评论 0 0