守護進程是一個黑色地帶的產(chǎn)物乔夯,無論是通過native的方式在linux中fork進程達到,還是在java層通過兩個service守護的方式垢村,都是不太友好的做法,據(jù)很多人反應(yīng)嚎卫,總有一些實際的業(yè)務(wù)場景中嘉栓,希望自己的應(yīng)用保持live狀態(tài), 一種是在native中做:
- linux中多進程驰凛;
- unix domain套接字實現(xiàn)跨進程通信;
- linux的信號處理担扑;
- exec函數(shù)族的用法恰响;
把他們組合起來實現(xiàn)了一個雙進程守護,幾個實現(xiàn)雙進程守護時的關(guān)鍵點:
父進程如何監(jiān)視到子進程(監(jiān)視進程)的死亡涌献?很簡單胚宦,在linux中,子進程被終止時燕垃,會向父進程發(fā)送SIG_CHLD信號枢劝,于是我們可以安裝信號處理函數(shù),并在此信號處理函數(shù)中重新啟動創(chuàng)建監(jiān)視進程卜壕;
子進程(監(jiān)視進程)如何監(jiān)視到父進程死亡您旁?當父進程死亡以后,子進程就成為了孤兒進程由Init進程領(lǐng)養(yǎng)轴捎,于是我們可以在一個循環(huán)中讀取子進程的父進程PID鹤盒,當變?yōu)?就說明其父進程已經(jīng)死亡,于是可以重啟父進程侦副。這里因為采用了循環(huán)侦锯,所以就引出了之前提到的耗電量的問題。
父子進程間的通信有一種辦法是父子進程間建立通信通道秦驯,然后通過監(jiān)視此通道來感知對方的存在尺碰,這樣不會存在之前提到的耗電量的問題,在本文的實現(xiàn)中,為了簡單亲桥,還是采用了輪詢父進程PID的辦法洛心,但是還是留出了父子進程的通信通道,雖然暫時沒有用到两曼,但可備不時之需皂甘!
這種native方式,可參考鏈接:http://dearseven.blog.163.com/blog/static/100537922201523143957103/
今天介紹下用兩個service守護的方式作一完整的小案例悼凑。僅作學(xué)習(xí)交流之用偿枕。兩個進程互相監(jiān)視對方,發(fā)現(xiàn)對方掛掉就立刻重啟户辫!(實際就是在onDisconnected時渐夸,start另一個service)
假設(shè)我們的APP中開啟了兩個Service,分別是A和B渔欢,那么:如果A守護B墓塌,則B掛掉的同時,A就應(yīng)該把B喚醒起來奥额,反之亦然苫幢,也就是說A和B應(yīng)該是互相守護,無論誰被殺掉垫挨,對方就把它喚醒起來韩肝。既然提到了兩個Service,那么這兩個Service就不能讓它們同處在一個進程中九榔,否則就會被一次性雙殺哀峻。顯然不能在同一個進程中,在android中通常我們可以使用AIDL來實現(xiàn)IPC實現(xiàn)哲泊。
原理圖(簡單版):
- MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 啟動兩個守護服務(wù)
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("守護服務(wù)A啟動中")
.setContentText("我是來守護B不被殺的!")
.setContentTitle("守護服務(wù)A")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(mPendingIntent)
.setWhen(System.currentTimeMillis());
Notification notification = builder.build();
// 設(shè)置service為前臺進程剩蟀,避免手機休眠時系統(tǒng)自動殺掉該服務(wù)
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) {
// 連接出現(xiàn)了異常斷開了罚勾,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("守護服務(wù)B啟動中")
.setContentText("我是來守護A不被殺的!")
.setContentTitle("守護服務(wù)B")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(mPendingIntent)
.setWhen(System.currentTimeMillis());
Notification notification = builder.build();
//設(shè)置service為前臺進程踢关,避免手機休眠時系統(tǒng)自動殺掉該服務(wù)
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) {
// 連接出現(xiàn)了異常斷開了,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;
}
}
最后:如果系統(tǒng)干掉這個服務(wù)疫萤,還是難逃此劫的先朦。向ROM廠商提出加白名單方式且预,才是終極最萬全方案。
以上完整代碼下載鏈接:https://github.com/hejunlin2013/MultiMediaSample