說道服務不被殺死說白了分兩種情況,一種是永遠在執行,不執行ondestory,還有一種方法就是在銷燬後重新執行oncreae方法,重新啓動,相比而言,第一種基本上時候夠嗆了,現在的清理軟件的優先級非常的高,所以,,,你懂得,你基本上無法創建一個永遠不死的,但是可以重生啊!
下邊上乾貨:
1,通過service中的onstartCommand方法,他其中有一個參數是flag,他有四種標誌,分別爲:
START_STICKY:重啓服務,如果沒有傳遞給他一個intent,那麼intent爲null,
START_NOT_STICKY:如果不進行傳遞intent,將不會啓動,所以不會收到null的intent.
START_REDELIVER_INTENT:重啓後將重新的intent傳遞過來。
START_STICKY_COMPATIBILITY是START_STICKY的兼容版本,不保證一定會重啓,所以建議使用第一個標誌,
在使用的時候將onstartCommand方法分局不同的需求返回不同的標誌即可,首先第一個標誌。
2,通過在service中在自己的ondestory方法中,啓動oncreate方法即可,這樣也是非常簡單的
3,通過更改優先級的方式,在清單文件中,將優先級更改爲1000,應該就差不多了,在清理內存的時候,優先級越高的越不容易被殺死。
4,通過發送廣播,在服務中,當服務銷燬的時候發送條廣播,廣播接收器接收到廣播的時候,重新啓動服務
5,創建定時器,通過定時啓動任務,每隔一定時間重新啓動一次服務即可
6,通過將service更改爲前臺服務也可以避免被殺死
其實方法還有很多,只要保證在服務被殺死之前再通過一定的手段讓服務重啓即可達到目的。
在這裏,我個人建議使用雙進程來完成,思路是:創建兩個進程,每隔進程相互判斷並傳遞消息,一旦有一個死亡,另一個馬上啓動治療模式,將其重生,大家如果細心地就能發現,你手機中的軟件,如微信等,都是通過類似這種方式實現的。這樣高大上啊:
下面本人將代碼貼出,並附上詳細的備註:
先創建aidl:(不懂得先學習aidl的使用)
<span style="font-size:14px;">package com.lzg.strongservice.service;
interface StrongService{
void startService();
void stopService();
}</span>
創建服務1:通過服務1,來對服務2進行跨進程操作。
//創建服務1,
public class Service1 extends Service {
//handler用於接收消息
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
//當收到發送的消息是,啓動服務2.
startService2();
break;
default:
break;
}
};
};
/**
* 使用aidl 啓動Service2
這是aidl接口實例,接口中定義兩個方法,一個是停止服務的方法,一個是開始服務的方法。
*/
private StrongService startS2 = new StrongService.Stub() {
@Override
public void stopService() throws RemoteException {
當調用停止服務時,停止服務2,
Intent i = new Intent(getBaseContext(), Service2.class);
getBaseContext().stopService(i);
}
@Override
public void startService() throws RemoteException {
//當調用啓動服務時,啓動服務2.
Intent i = new Intent(getBaseContext(), Service2.class);
getBaseContext().startService(i);
}
};
/**
* 在內存緊張的時候,系統回收內存時,會回調OnTrimMemory, 重寫onTrimMemory當系統清理內存時從新啓動Service2
*/
@Override
public void onTrimMemory(int level) {
/*
* 啓動service2
*/
startService2();
}
@Override
public void onCreate() {
Toast.makeText(Service1.this, "Service1 正在啓動...", Toast.LENGTH_SHORT)
.show();
//在啓動服務1的時候,將服務2也啓動,並監聽服務2
startService2();
/*
* 此線程用監聽Service2的狀態
*/
new Thread() {
public void run() {
while (true) {
//判斷服務2是否已經啓動,如果沒有,則發送一個消息給handler.
boolean isRun = Utils.isServiceWork(Service1.this,
"com.lzg.strongservice.service.Service2");
if (!isRun) {
Message msg = Message.obtain();
msg.what = 1;
handler.sendMessage(msg);
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}.start();
}
/**
* 判斷Service2是否還在運行,如果不是則啓動Service2
*/
private void startService2() {
boolean isRun = Utils.isServiceWork(Service1.this,
"com.lzg.strongservice.service.Service2");
if (isRun == false) {
try {
startS2.startService();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//這個方法就是使用了重器標誌
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return (IBinder) startS2;
}
}
創建服務2,方法和服務1類似,達到相互控制監聽的作用(註釋只寫了一份,意思相同):
public class Service2 extends Service {
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
startService1();
break;
default:
break;
}
};
};
/**
* 使用aidl 啓動Service1
*/
private StrongService startS1 = new StrongService.Stub() {
@Override
public void stopService() throws RemoteException {
Intent i = new Intent(getBaseContext(), Service1.class);
getBaseContext().stopService(i);
}
@Override
public void startService() throws RemoteException {
Intent i = new Intent(getBaseContext(), Service1.class);
getBaseContext().startService(i);
}
};
/**
* 在內存緊張的時候,系統回收內存時,會回調OnTrimMemory, 重寫onTrimMemory當系統清理內存時從新啓動Service1
*/
@Override
public void onTrimMemory(int level) {
startService1();
}
@SuppressLint("NewApi")
public void onCreate() {
Toast.makeText(Service2.this, "Service2 啓動中...", Toast.LENGTH_SHORT)
.show();
startService1();
/*
* 此線程用監聽Service2的狀態
*/
new Thread() {
public void run() {
while (true) {
boolean isRun = Utils.isServiceWork(Service2.this,
"com.lzg.strongservice.service.Service1");
if (!isRun) {
Message msg = Message.obtain();
msg.what = 1;
handler.sendMessage(msg);
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}.start();
}
/**
* 判斷Service1是否還在運行,如果不是則啓動Service1
*/
private void startService1() {
boolean isRun = Utils.isServiceWork(Service2.this,
"com.lzg.strongservice.service.Service1");
if (isRun == false) {
try {
startS1.startService();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return (IBinder) startS1;
}
}</span></strong>