
守護(hù)進(jìn)程是一個(gè)黑色地帶的產(chǎn)物,無(wú)論是通過(guò)native的方式在linux中fork進(jìn)程達(dá)到,還是在java層通過(guò)兩個(gè)service守護(hù)的方式,都是不太友好的做法,據(jù)很多人反應(yīng),總有一些實(shí)際的業(yè)務(wù)場(chǎng)景中,希望自己的應(yīng)用保持live狀態(tài), 一種是在native中做:
- linux中多進(jìn)程;
- unix domain套接字實(shí)現(xiàn)跨進(jìn)程通信;
- linux的信號(hào)處理;
- exec函數(shù)族的用法;
把他們組合起來(lái)實(shí)現(xiàn)了一個(gè)雙進(jìn)程守護(hù),幾個(gè)實(shí)現(xiàn)雙進(jìn)程守護(hù)時(shí)的關(guān)鍵點(diǎn):
父進(jìn)程如何監(jiān)視到子進(jìn)程(監(jiān)視進(jìn)程)的死亡?很簡(jiǎn)單,在linux中,子進(jìn)程被終止時(shí),會(huì)向父進(jìn)程發(fā)送SIG_CHLD信號(hào),于是我們可以安裝信號(hào)處理函數(shù),并在此信號(hào)處理函數(shù)中重新啟動(dòng)創(chuàng)建監(jiān)視進(jìn)程;
子進(jìn)程(監(jiān)視進(jìn)程)如何監(jiān)視到父進(jìn)程死亡?當(dāng)父進(jìn)程死亡以后,子進(jìn)程就成為了孤兒進(jìn)程由Init進(jìn)程領(lǐng)養(yǎng),于是我們可以在一個(gè)循環(huán)中讀取子進(jìn)程的父進(jìn)程PID,當(dāng)變?yōu)?就說(shuō)明其父進(jìn)程已經(jīng)死亡,于是可以重啟父進(jìn)程。這里因?yàn)椴捎昧搜h(huán),所以就引出了之前提到的耗電量的問(wèn)題。
父子進(jìn)程間的通信有一種辦法是父子進(jìn)程間建立通信通道,然后通過(guò)監(jiān)視此通道來(lái)感知對(duì)方的存在,這樣不會(huì)存在之前提到的耗電量的問(wèn)題,在本文的實(shí)現(xiàn)中,為了簡(jiǎn)單,還是采用了輪詢父進(jìn)程PID的辦法,但是還是留出了父子進(jìn)程的通信通道,雖然暫時(shí)沒有用到,但可備不時(shí)之需!
這種native方式,可參考鏈接:http://dearseven.blog.163.com/blog/static/100537922201523143957103/
今天介紹下用兩個(gè)service守護(hù)的方式作一完整的小案例。僅作學(xué)習(xí)交流之用。兩個(gè)進(jìn)程互相監(jiān)視對(duì)方,發(fā)現(xiàn)對(duì)方掛掉就立刻重啟!(實(shí)際就是在onDisconnected時(shí),start另一個(gè)service)
假設(shè)我們的APP中開啟了兩個(gè)Service,分別是A和B,那么:如果A守護(hù)B,則B掛掉的同時(shí),A就應(yīng)該把B喚醒起來(lái),反之亦然,也就是說(shuō)A和B應(yīng)該是互相守護(hù),無(wú)論誰(shuí)被殺掉,對(duì)方就把它喚醒起來(lái)。既然提到了兩個(gè)Service,那么這兩個(gè)Service就不能讓它們同處在一個(gè)進(jìn)程中,否則就會(huì)被一次性雙殺。顯然不能在同一個(gè)進(jìn)程中,在android中通常我們可以使用AIDL來(lái)實(shí)現(xiàn)IPC實(shí)現(xiàn)。
原理圖(簡(jiǎn)單版):

- MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 啟動(dòng)兩個(gè)守護(hù)服務(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("守護(hù)服務(wù)A啟動(dòng)中")
.setContentText("我是來(lái)守護(hù)B不被殺的!")
.setContentTitle("守護(hù)服務(wù)A")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(mPendingIntent)
.setWhen(System.currentTimeMillis());
Notification notification = builder.build();
// 設(shè)置service為前臺(tái)進(jìn)程,避免手機(jī)休眠時(shí)系統(tǒng)自動(dò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();
// 啟動(dòng)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("守護(hù)服務(wù)B啟動(dòng)中")
.setContentText("我是來(lái)守護(hù)A不被殺的!")
.setContentTitle("守護(hù)服務(wù)B")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(mPendingIntent)
.setWhen(System.currentTimeMillis());
Notification notification = builder.build();
//設(shè)置service為前臺(tái)進(jìn)程,避免手機(jī)休眠時(shí)系統(tǒng)自動(dò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();
// 啟動(dòng)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)干掉這個(gè)服務(wù),還是難逃此劫的。向ROM廠商提出加白名單方式,才是終極最萬(wàn)全方案。
以上完整代碼下載鏈接:https://github.com/hejunlin2013/MultiMediaSample