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