两个服务的方式实现双进程守护

其实双进程守护,保证我们的应用不会被安全软件杀死,的关键在于我们使用了两个进程,并且是他们是相互绑定的,但是测试发现这种做法,对于小米手机来说,并不起作用。我听说有人使用同步数据的方式来唤醒死掉的进程,或者是使用NDK方式来保护我们的应用,这两种方式,下次再分享。

远程服务

package com.example.mypc.doubleprocess;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

/**
* Created by MyPC on 2016/10/9.
*/

public class RemoteService extends Service {

private Binder mBinder;
private MyConn mConn;
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

@Override
public void onCreate() {
    mBinder=new MyBinder();
    if(mConn==null){
        mConn=new MyConn();
    }
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    //第一次绑定对方。
    RemoteService.this.bindService(new Intent(this,LocalService.class),mConn,BIND_IMPORTANT);

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

class  MyBinder extends IProcessService.Stub{


    public String getServiceName(){
        return  "RemoteService";
    }
}

class  MyConn implements ServiceConnection{

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        Log.i("TAG","远程链接本地服务成功");
        Toast.makeText(RemoteService.this,"链接本地服务成功",Toast.LENGTH_LONG).show();


    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
            //对方(localService)被干掉了
        Log.i("TAG","localService服务被杀");
        Toast.makeText(RemoteService.this,"localService服务被杀",Toast.LENGTH_LONG).show();
        //启动对方(对方会走onStart绑定我)
        RemoteService.this.startService(new Intent(RemoteService.this,LocalService.class));
        //绑定对方(我再绑定对方)
        RemoteService.this.bindService(new Intent(RemoteService.this,LocalService.class),mConn, Context.BIND_IMPORTANT);
    }
}

}

本地服务

package com.example.mypc.doubleprocess;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

/**
* Created by MyPC on 2016/10/9.
*/

public class LocalService extends Service {
private Binder mBinder;
private MyConn mConn;

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

@Override
public void onCreate() {
    mBinder=new MyBinder();
    if(mConn==null){
        mConn=new MyConn();
    }
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    //第一次绑定对方。
    //再第二次还会绑定一次
    LocalService.this.bindService(new Intent(this,RemoteService.class),mConn,BIND_IMPORTANT);
    return super.onStartCommand(intent, flags, startId);
}

class  MyBinder extends IProcessService.Stub{

    public String getServiceName(){
         return  "LocalService";
    }
}
class MyConn implements ServiceConnection{

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        Log.i("TAG","本地链接远程服务成功");
        Toast.makeText(LocalService.this,"链接远程服务成功",Toast.LENGTH_LONG).show();

    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        //对方(remoteService)被干掉了
        Log.i("TAG","remoteService服务被杀");
        Toast.makeText(LocalService.this,"remoteService服务被杀",Toast.LENGTH_LONG).show();
        //第二次启动对方(对方会走onStart再绑定我)
        LocalService.this.startService(new Intent(LocalService.this,RemoteService.class));
        //第二次绑定对方(我可以再次绑定对方)
        LocalService.this.bindService(new Intent(LocalService.this,RemoteService.class),mConn, Context.BIND_IMPORTANT);
    }
}

}

aidl

// IMyAidlInterface.aidl
package com.example.mypc.doubleprocess;

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

interface IProcessService {
String getServiceName();
}

MainActivity

package com.example.mypc.doubleprocess;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //第一次启动
    this.startService(new Intent(MainActivity.this,LocalService.class));
    //第一次启动
    this.startService(new Intent(MainActivity.this,RemoteService.class));
}

}

Mainifest

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值