美文网首页
进程保活之双进程守护

进程保活之双进程守护

作者: zsj1225 | 来源:发表于2017-06-17 16:12 被阅读135次

上一篇文章讲到一个像素保活线程。没有看的小伙伴可以去看看。地址: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配置:

进程保活之双进程守护

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

RemoteConn.aidl

package com.zsj.doubleprocess;

interface RemoteConn {
    String getProcessName();
}

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

优点:

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

弊端:

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

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

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

相关文章

  • 进程保活之双进程守护

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

  • 进程保活:双进程守护 + JobScheduler

    创建 src/main/aidl 文件夹,在其中新建 .aidl 文件 1. 使用:在 MainActivity ...

  • 经验总结 - 收藏集 - 掘金

    安卓开发:使用双进程守护和进程提权来实现服务进程保活 - Android - 掘金在 如何让我们的Android应...

  • 进程保活与拉活

    进程相关知识梳理 Activity 1像素保活 前台服务保活 账户同步拉活 JobScheduler 拉活 双进程...

  • Android保活系列之——双进程守护

    本文为个人学习笔记分享,没有任何商业化行为,对其他文章的引用都会标记。如有侵权行为,请及时提醒更正!如需转载请表明...

  • Android 进程保活 双守护进程互拉

    先甩个源码:https://github.com/karedem/dirtyService

  • 安卓进程保活

    1、通过各大手机厂商提供的推送服务,进行唤醒保活; 2、双进程相互守护保活,如果是手机有一键清理,就双双死掉了;不...

  • android保活进程总结--双进程保活策略

    进程的保活,在很多资讯类的App和即时通讯App的用处很大,奈何谷歌的推送服务在国内是被阉割了!据说是在8.0(奥...

  • 第十六周 进程保活

    话题:进程保活 这个问题时常在面试中被问到关键字:Android 进程保活招式大全 参考答案 1.进程保活方案 -...

  • 进程保活方案学习

    进程保活方案 进程保活主要有两个方案 提高进程优先级,降低死亡几率 在进程被杀死后进行拉活 进程为什么会死亡 从L...

网友评论

      本文标题:进程保活之双进程守护

      本文链接:https://www.haomeiwen.com/subject/wpqbqxtx.html