Android中的泵林迹活是一個(gè)永不過時(shí)的話題,因?yàn)槊恳粋€(gè)APP都希望能在后臺(tái)不停的運(yùn)行去搜集用戶數(shù)據(jù)祥得,在Android 系統(tǒng)處于較低版本的時(shí)候(目前最新版本為12兔沃,較低版本指的是8以下),很多APP借助于系統(tǒng)層面的漏洞研發(fā)出了各種奔都埃活的方法乒疏,但是隨著Android 版本的不斷更新,過往的币梗活方法漸漸失效怕吴,Android中的保活成為了一個(gè)越來越難辦到的事情县踢,但是我認(rèn)為這是一個(gè)好事械哟,如非這樣你永遠(yuǎn)不知道你的手機(jī)后臺(tái)到底有多少APP背著你干了多少事情。當(dāng)然殿雪,系統(tǒng)的事情不是我們能掌控的了的暇咆。那么,我們先來看看以前為了北铮活都有哪些手段爸业。
手段一:在Service的onStartCommand方法中返回****START_STICKY****(****親測無效****)
在Service的onStartCommand方法中返回鍵有下面幾種可供選擇:
(1)START_STICKY:如果Service所在的進(jìn)程,在執(zhí)行了onStartCommand方法后亏镰,被清理了扯旷,那么這個(gè)Service會(huì)被保留在已開始的狀態(tài),但是不保留傳入的Intent索抓,隨后系統(tǒng)會(huì)嘗試重新創(chuàng)建此Service钧忽。
(2)START_NOT_STICKY:如果Service所在的進(jìn)程毯炮,在執(zhí)行了onStartCommand方法后,被清理了耸黑,則系統(tǒng)不會(huì)重新啟動(dòng)此Service桃煎。
(3)START_REDELIVER_INTENT:如果Service所在的進(jìn)程,在執(zhí)行了onStartCommand方法后大刊,被清理了为迈,則結(jié)果和START_STICKY一樣,也會(huì)重新創(chuàng)建此Service并調(diào)用onStartCommand方法缺菌。不同之處在于葫辐,如果是返回的是START_REDELIVER_INTENT ,則重新創(chuàng)建Service時(shí)onStartCommand方法會(huì)傳入之前的intent伴郁。
手段二:在清單文件里面設(shè)置優(yōu)先級(jí)****(****親測無效****)
手段三:在Service即將銷毀的時(shí)候重新啟動(dòng)****(****親測無效****)
手段四:借助AIDL使用雙進(jìn)程惫⒄剑活****(****親測無效****)
首先創(chuàng)建一個(gè)AIDL文件
package com.steven.activitydemo;
// Declare any non-default types here with import statements
interface ProcessConnection {
String getServiceName();
}
創(chuàng)建本地服務(wù)
public class LocalService extends Service {
private MyBinder mBinder;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
ProcessConnection iMyAidlInterface = ProcessConnection.Stub.asInterface(service);
try {
Log.i("LocalService", "connected with " + iMyAidlInterface.getServiceName());
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("stevens", "鏈接斷開,重新啟動(dòng) RemoteService");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(new Intent(LocalService.this, RemoteService.class));
} else {
startService(new Intent(LocalService.this, RemoteService.class));
}
bindService(new Intent(LocalService.this, RemoteService.class), connection, Context.BIND_IMPORTANT);
}
};
public LocalService() {
}
@Override
public void onCreate() {
super.onCreate();
TimeTool.getInstance();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("stevens", "LocalService 啟動(dòng)");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(new Intent(LocalService.this, RemoteService.class));
} else {
startService(new Intent(LocalService.this, RemoteService.class));
}
bindService(new Intent(this, RemoteService.class), connection, Context.BIND_IMPORTANT);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(connection);
Log.i("stevens", "-----local onDestroy");
}
@Override
public IBinder onBind(Intent intent) {
mBinder = new MyBinder();
return mBinder;
}
private class MyBinder extends ProcessConnection.Stub {
@Override
public String getServiceName() throws RemoteException {
return LocalService.class.getName();
}
}
}
創(chuàng)建遠(yuǎn)程服務(wù):
public class RemoteService extends Service {
private MyBinder mBinder;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
ProcessConnection iMyAidlInterface = ProcessConnection.Stub.asInterface(service);
try {
Log.i("RemoteService", "connected with " + iMyAidlInterface.getServiceName());
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("stevens", "鏈接斷開焊傅,重新啟動(dòng) LocalService");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(new Intent(RemoteService.this, LocalService.class));
} else {
startService(new Intent(RemoteService.this, LocalService.class));
}
bindService(new Intent(RemoteService.this, LocalService.class), connection, Context.BIND_IMPORTANT);
}
};
public RemoteService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("stevens", "RemoteService 啟動(dòng)");
bindService(new Intent(this, LocalService.class), connection, Context.BIND_IMPORTANT);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(connection);
Log.i("stevens", "-----remote onDestroy");
}
@Override
public IBinder onBind(Intent intent) {
mBinder = new MyBinder();
return mBinder;
}
private class MyBinder extends ProcessConnection.Stub {
@Override
public String getServiceName() throws RemoteException {
return RemoteService.class.getName();
}
}
}
最后在清單文件聲明:
<service
android:name=".doubleservice.RemoteService"
android:enabled="true"
android:exported="true"
android:process=":RemoteProcess" />
<service
android:name=".doubleservice.LocalService"
android:enabled="true"
android:exported="true" />
手段五:1像素的Activity讓應(yīng)用在熄屏后崩セ活****(****親測無效****)
具體怎么實(shí)現(xiàn)可以參照這篇文章
<u>https://blog.csdn.net/zhenufo/article/details/79317068</u>
運(yùn)行一段時(shí)間后系統(tǒng)會(huì)自動(dòng)殺死整個(gè)APP
手段六:****開啟前臺(tái)服務(wù)(親測有效)
在Service的onCreate方法中開啟前臺(tái)服務(wù)
當(dāng)然,APP弊夤冢活的方式方法遠(yuǎn)不止這些鹏倘,但是隨著Android 系統(tǒng)的不斷優(yōu)化,蓖绲活現(xiàn)在越來越不太現(xiàn)實(shí)纤泵,但是我們可以盡量提高我們APP的優(yōu)先級(jí)讓系統(tǒng)不輕易殺死我們的APP,這一點(diǎn)還是可以辦到的镜粤。