上一篇文章講到一個(gè)像素保活線程睡榆。沒有看的小伙伴可以去看看萍肆。地址:http://www.reibang.com/p/a61ecc016aa5
原理:
殺進(jìn)程是一個(gè)一個(gè)殺的。我們可以定義兩個(gè)進(jìn)程胀屿,兩個(gè)進(jìn)程相互監(jiān)聽塘揣。A進(jìn)程被殺死,B進(jìn)程監(jiān)聽到就啟動(dòng)A進(jìn)程宿崭。否則相反亲铡。
實(shí)現(xiàn):
1,創(chuàng)建MainActivity。一上來就啟動(dòng)兩個(gè)服務(wù)奖蔓。
package com.zsj.doubleprocess;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 一上來就啟動(dòng)兩個(gè)服務(wù)琅摩。
startService(new Intent(this, LocalService.class));
startService(new Intent(this, RemoteService.class));
}
}
2,創(chuàng)建兩個(gè)進(jìn)程的服務(wù)LocalService和RemoteService锭硼,并相互綁定。在連接斷開的啟動(dòng)對(duì)方的進(jìn)程的服務(wù)蜕劝。
LocalService.java
package com.zsj.doubleprocess;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
/**
* 第一個(gè)服務(wù)檀头。本地服務(wù)
*
* @author zsj
*
*/
public class LocalService extends Service {
private MyBind bind;
private MyServiceConnection conn;
@Override
public void onCreate() {
super.onCreate();
if (bind == null) {
bind = new MyBind();
}
if (conn == null) {
conn = new MyServiceConnection();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 綁定另外進(jìn)程的服務(wù)RemoteService,并設(shè)置BIND_IMPORTANT為重要綁定
bindService(new Intent(LocalService.this, RemoteService.class), conn,
Context.BIND_IMPORTANT);
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return bind;
}
public class MyBind extends RemoteConn.Stub {
@Override
public String getProcessName() throws RemoteException {
return "LocalService";
}
}
public class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 遠(yuǎn)程服務(wù)連接成功回調(diào)
}
@Override
public void onServiceDisconnected(ComponentName name) {
// 被綁定的服務(wù)斷開連接的時(shí)候回調(diào)
// 啟動(dòng)被綁定的服務(wù)。
startService(new Intent(LocalService.this, RemoteService.class));
bindService(new Intent(LocalService.this, RemoteService.class),
conn, Context.BIND_IMPORTANT);
}
}
}
RemoteService.java
package com.zsj.doubleprocess;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
public class RemoteService extends Service {
private MyBind bind;
private MyServiceConnection conn;
@Override
public void onCreate() {
super.onCreate();
if (bind == null) {
bind = new MyBind();
}
if (conn == null) {
conn = new MyServiceConnection();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 綁定另外進(jìn)程的服務(wù)LocalService,并設(shè)置BIND_IMPORTANT為重要綁定
bindService(new Intent(RemoteService.this, LocalService.class), conn,
Context.BIND_IMPORTANT);
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return bind;
}
public class MyBind extends RemoteConn.Stub {
@Override
public String getProcessName() throws RemoteException {
return "RemoteService";
}
}
public class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 遠(yuǎn)程服務(wù)連接成功回調(diào)
}
@Override
public void onServiceDisconnected(ComponentName name) {
// 被綁定的服務(wù)斷開連接的時(shí)候回調(diào)
// 啟動(dòng)被綁定的服務(wù)岖沛。
startService(new Intent(RemoteService.this, LocalService.class));
bindService(new Intent(RemoteService.this, LocalService.class),
conn, Context.BIND_IMPORTANT);
}
}
}‘
3,因?yàn)閮蓚€(gè)服務(wù)在不同的進(jìn)程里面.所以在AndroidManifest.xml配置:
4.在不同的進(jìn)程通信暑始。用到Aidl.定義一個(gè)通信接口RemoteConn.aidl。
RemoteConn.aidl
package com.zsj.doubleprocess;
interface RemoteConn {
String getProcessName();
}
雙進(jìn)程守護(hù)就完成了婴削。
因?yàn)檫@兩個(gè)服務(wù)都是后臺(tái)服務(wù)廊镜。為了提高更加不容易被殺死“λ祝可以提升為前臺(tái)服務(wù)嗤朴。就不給實(shí)現(xiàn)了。很簡單虫溜。
優(yōu)點(diǎn):
可以防止360等第三方清理工具殺死雹姊。
弊端:
但是當(dāng)用戶手動(dòng)點(diǎn)擊強(qiáng)制停止。進(jìn)程真的就停止了衡楞。
下次寫就算用戶手動(dòng)點(diǎn)擊強(qiáng)制停止吱雏。也能自動(dòng)喚醒進(jìn)程。