Android双进程守护
Android通过启动两个进程相互绑定监督:
1.创建两个进程
首先创建两个服务,并将其中一个设置为远程服务
<service android:name=".LocalService" /> <service android:name=".RemoteService" android:process=".remote" />
然后看手机进程效果:
已成功启动两个进程!
2.两个进程相互绑定
进程间通信需用到aidl:
// ServiceProcess.aidl package com.doubleprocess; // Declare any non-default types here with import statements interface ProcessService { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ String getProcessName(); }
创建MyBinder继承aidl
class MyBinder extends ProcessService.Stub { @Override public String getProcessName() throws RemoteException { return "LocalService"; } }
本地服务LocaService启动时绑定RomoteService,RomoteService启动时也绑定LocaService
@Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); Log.i("LocalService", "onStart"); this.bindService(new Intent(this, RemoteService.class), conn, Context.BIND_IMPORTANT); }
@Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); Log.i("RemoteService", "onStart"); this.bindService(new Intent(this, LocalService.class), conn, Context.BIND_IMPORTANT); }
在LocaService与RomoteService连接发生异常的时候,重新启动本服务和绑定的服务!
class MyConn implements ServiceConnection { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { Log.i("LocalService", "远程服务启动"); } @Override public void onServiceDisconnected(ComponentName componentName) { Log.i("LocalService", "远程服务断开"); LocalService.this.startService(new Intent(LocalService.this, RemoteService.class)); LocalService.this.bindService(new Intent(LocalService.this, RemoteService.class), conn, Context.BIND_IMPORTANT); } }
本地服务LocaService
public class LocalService extends Service { private MyBinder binder; private MyConn conn; @Nullable @Override public IBinder onBind(Intent intent) { Log.i("LocalService", "onBind"); return binder; } @Override public void onCreate() { super.onCreate(); Log.i("LocalService", "onCreate"); binder = new MyBinder(); if (conn == null) { conn = new MyConn(); } } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); Log.i("LocalService", "onStart"); this.bindService(new Intent(this, RemoteService.class), conn, Context.BIND_IMPORTANT); } class MyBinder extends ProcessService.Stub { @Override public String getProcessName() throws RemoteException { return "LocalService"; } } class MyConn implements ServiceConnection { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { Log.i("LocalService", "远程服务启动"); } @Override public void onServiceDisconnected(ComponentName componentName) { Log.i("LocalService", "远程服务断开"); LocalService.this.startService(new Intent(LocalService.this, RemoteService.class)); LocalService.this.bindService(new Intent(LocalService.this, RemoteService.class), conn, Context.BIND_IMPORTANT); } } @Override public void onDestroy() { super.onDestroy(); unbindService(conn); } }
远程服务RemoteService
public class RemoteService extends Service { private MyBinder binder; private MyConn conn; @Nullable @Override public IBinder onBind(Intent intent) { Log.i("RemoteService", "onBind"); return binder; } @Override public void onCreate() { super.onCreate(); Log.i("RemoteService", "onCreate"); binder = new MyBinder(); if (conn == null) { conn = new MyConn(); } } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); Log.i("RemoteService", "onStart"); this.bindService(new Intent(this, LocalService.class), conn, Context.BIND_IMPORTANT); } class MyBinder extends ProcessService.Stub { @Override public String getProcessName() throws RemoteException { return "RemoteService"; } } class MyConn implements ServiceConnection { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { Log.i("RemoteService", "本地服务启动"); } @Override public void onServiceDisconnected(ComponentName componentName) { Log.i("RemoteService", "本地服务断开"); RemoteService.this.startService(new Intent(RemoteService.this, LocalService.class)); RemoteService.this.bindService(new Intent(RemoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT); } } @Override public void onDestroy() { super.onDestroy(); unbindService(conn); } }