守护进程是一个黑色地带的产物,无论是通过native的方式在linux中fork进程达到,还是在java层通过两个service守护的方式,都是不太友好的做法,据很多人反应,总有一些实际的业务场景中,希望自己的应用保持live状态, 一种是在native中做:
- linux中多进程;
- unix domain套接字实现跨进程通信;
- linux的信号处理;
- exec函数族的用法;
把他们组合起来实现了一个双进程守护,几个实现双进程守护时的关键点:
父进程如何监视到子进程(监视进程)的死亡?很简单,在linux中,子进程被终止时,会向父进程发送SIG_CHLD信号,于是我们可以安装信号处理函数,并在此信号处理函数中重新启动创建监视进程;
子进程(监视进程)如何监视到父进程死亡?当父进程死亡以后,子进程就成为了孤儿进程由Init进程领养,于是我们可以在一个循环中读取子进程的父进程PID,当变为1就说明其父进程已经死亡,于是可以重启父进程。这里因为采用了循环,所以就引出了之前提到的耗电量的问题。
父子进程间的通信有一种办法是父子进程间建立通信通道,然后通过监视此通道来感知对方的存在,这样不会存在之前提到的耗电量的问题,在本文的实现中,为了简单,还是采用了轮询父进程PID的办法,但是还是留出了父子进程的通信通道,虽然暂时没有用到,但可备不时之需!
这种native方式,可参考链接:http://dearseven.blog.163.com/blog/static/100537922201523143957103/
今天介绍下用两个service守护的方式作一完整的小案例。仅作学习交流之用。两个进程互相监视对方,发现对方挂掉就立刻重启!(实际就是在onDisconnected时,start另一个service)
假设我们的APP中开启了两个Service,分别是A和B,那么:如果A守护B,则B挂掉的同时,A就应该把B唤醒起来,反之亦然,也就是说A和B应该是互相守护,无论谁被杀掉,对方就把它唤醒起来。既然提到了两个Service,那么这两个Service就不能让它们同处在一个进程中,否则就会被一次性双杀。显然不能在同一个进程中,在android中通常我们可以使用AIDL来实现IPC实现。
原理图(简单版):
- MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 启动两个守护服务
startService(new Intent(this, ServiceA.class));
startService(new Intent(this, ServiceB.class));
}
}
- manifest
<service android:name=".ServiceA"></service>
<service android:name=".ServiceB" android:process="com.guardprocess.remote"></service>
- aide
interface IBridgeInterface {
String getName();
}
- ServiceA
public class ServiceA extends Service {
private static final String TAG = ServiceA.class.getSimpleName();
private MyBinder mBinder;
private PendingIntent mPendingIntent;
private MyServiceConnection mServiceConnection;
@Override
public void onCreate() {
super.onCreate();
if (mBinder == null) {
mBinder = new MyBinder();
}
mServiceConnection = new MyServiceConnection();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.bindService(new Intent(this, ServiceB.class), mServiceConnection, Context.BIND_IMPORTANT);
mPendingIntent = PendingIntent.getService(this, 0, intent, 0);
Notification.Builder builder = new Notification.Builder(this);
builder.setTicker("守护服务A启动中")
.setContentText("我是来守护B不被杀的!")
.setContentTitle("守护服务A")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(mPendingIntent)
.setWhen(System.currentTimeMillis());
Notification notification = builder.build();
// 设置service为前台进程,避免手机休眠时系统自动杀掉该服务
startForeground(startId, notification);
return START_STICKY;
}
class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName componentName, IBinder binder) {
Log.i(TAG, "ServiceA连接成功");
Toast.makeText(ServiceA.this, "ServiceA连接成功", Toast.LENGTH_LONG).show();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
// 连接出现了异常断开了,RemoteService被杀掉了
Toast.makeText(ServiceA.this, "ServiceA被干掉", Toast.LENGTH_LONG).show();
// 启动ServiceB
ServiceA.this.startService(new Intent(ServiceA.this, ServiceB.class));
ServiceA.this.bindService(new Intent(ServiceA.this, ServiceB.class),
mServiceConnection, Context.BIND_IMPORTANT);
}
}
class MyBinder extends IBridgeInterface.Stub {
@Override
public String getName() throws RemoteException {
return "ServiceA";
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
- ServiceB
public class ServiceB extends Service {
private static final String TAG = ServiceB.class.getSimpleName();
private MyBinder mBinder;
private PendingIntent mPendingIntent;
private MyServiceConnection mServiceConnection;
@Override
public void onCreate() {
super.onCreate();
if (mBinder == null) {
mBinder = new MyBinder();
}
mServiceConnection = new MyServiceConnection();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.bindService(new Intent(this,ServiceA.class), mServiceConnection, Context.BIND_IMPORTANT);
mPendingIntent =PendingIntent.getService(this, 0, intent, 0);
Notification.Builder builder = new Notification.Builder(this);
builder.setTicker("守护服务B启动中")
.setContentText("我是来守护A不被杀的!")
.setContentTitle("守护服务B")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(mPendingIntent)
.setWhen(System.currentTimeMillis());
Notification notification = builder.build();
//设置service为前台进程,避免手机休眠时系统自动杀掉该服务
startForeground(startId, notification);
return START_STICKY;
}
class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName componentName, IBinder binder) {
Log.i(TAG, "ServiceB连接成功");
Toast.makeText(ServiceB.this, "ServiceB连接成功", Toast.LENGTH_LONG).show();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
// 连接出现了异常断开了,LocalCastielService被杀死了
Toast.makeText(ServiceB.this, "ServiceB被干掉", Toast.LENGTH_LONG).show();
// 启动ServiceA
ServiceB.this.startService(new Intent(ServiceB.this, ServiceA.class));
ServiceB.this.bindService(new Intent(ServiceB.this, ServiceA.class), mServiceConnection, Context.BIND_IMPORTANT);
}
}
class MyBinder extends IBridgeInterface.Stub {
@Override
public String getName() throws RemoteException {
return "ServiceB";
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
最后:如果系统干掉这个服务,还是难逃此劫的。向ROM厂商提出加白名单方式,才是终极最万全方案。
以上完整代码下载链接:https://github.com/hejunlin2013/MultiMediaSample