Android編程防止進(jìn)程被第三方軟件殺死的方法
本文實(shí)例講述了Android編程防止進(jìn)程被第三方軟件殺死的方法。分享給大家供大家參考,具體如下:
項(xiàng)目測(cè)試的時(shí)候發(fā)現(xiàn),按home鍵回到桌面,再用360清理內(nèi)存,軟件被結(jié)束,再次進(jìn)入的時(shí)候報(bào)錯(cuò),看了下log,以為是有的地方?jīng)]有控制好,但是又不知道360結(jié)束的是什么(這個(gè)現(xiàn)在還沒弄明白)。使用小米系統(tǒng)的進(jìn)程管理優(yōu)化內(nèi)存就不報(bào)錯(cuò)。
后來想到用Service防止軟件被kill掉,查了下資料,發(fā)現(xiàn)google 管方就有,F(xiàn)oregroundService 前臺(tái)服務(wù),讓服務(wù)一直以前臺(tái)任務(wù)的方式運(yùn)行,可以在service 的oncreate來實(shí)現(xiàn)前臺(tái)服務(wù), 通過這個(gè)方法必須發(fā)送一個(gè)通知欄,讓用戶知道服務(wù)在運(yùn)行。
Notification notification = new Notification(R.drawable.icon, "服務(wù)開啟", System.currentTimeMillis()); notification.flags|= Notification.FLAG_NO_CLEAR; notification.flags=Notification.FLAG_ONGOING_EVENT; Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "service", "防止服務(wù)被任務(wù)管理器所殺", pendingIntent); startForeground(ONGOING_NOTIFICATION, notification);
這樣就能保持service 運(yùn)行,可是通知欄不能清除 ,一清除就會(huì)被kill。
后來一次 做自定義Notification的時(shí)候,通知欄沒有顯示通知,查看后發(fā)現(xiàn) service 也沒被kill 。所以就進(jìn)一步去研究了下 最后發(fā)現(xiàn) 只用兩行代碼就能保持服務(wù)不會(huì)被kill,并且不會(huì)有通知欄通知代碼如下:
Notification notification = new Notification(); startForeground(1, notification);
完整代碼如下:
public class TestService extends Service {
private static final Class[] mStartForegroundSignature = new Class[] {
int.class, Notification.class };
private static final Class[] mStopForegroundSignature = new Class[] { boolean.class };
private NotificationManager mNM;
private Method mStartForeground;
private Method mStopForeground;
private Object[] mStartForegroundArgs = new Object[2];
private Object[] mStopForegroundArgs = new Object[1];
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mNM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
try {
mStartForeground = TestService.class.getMethod("startForeground",
mStartForegroundSignature);
mStopForeground = TestService.class.getMethod("stopForeground",
mStopForegroundSignature);
} catch (NoSuchMethodException e) {
mStartForeground = mStopForeground = null;
}
// 我們并不需要為 notification.flags 設(shè)置 FLAG_ONGOING_EVENT,因?yàn)?
// 前臺(tái)服務(wù)的 notification.flags 總是默認(rèn)包含了那個(gè)標(biāo)志位
Notification notification =new Notification();
// 注意使用 startForeground ,id 為 0 將不會(huì)顯示 notification
startForegroundCompat(1, notification);
}
@Override
public void onDestroy() {
super.onDestroy();
stopForegroundCompat(1);
}
// 以兼容性方式開始前臺(tái)服務(wù)
private void startForegroundCompat(int id, Notification n) {
if (mStartForeground != null) {
mStartForegroundArgs[0] = id;
mStartForegroundArgs[1] = n;
try {
mStartForeground.invoke(this, mStartForegroundArgs);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return;
}
mNM.notify(id, n);
}
// 以兼容性方式停止前臺(tái)服務(wù)
private void stopForegroundCompat(int id) {
if (mStopForeground != null) {
mStopForegroundArgs[0] = Boolean.TRUE;
try {
mStopForeground.invoke(this, mStopForegroundArgs);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return;
}
// 在 setForeground 之前調(diào)用 cancel,因?yàn)槲覀冇锌赡茉谌∠芭_(tái)服務(wù)之后
// 的那一瞬間被kill掉。這個(gè)時(shí)候 notification 便永遠(yuǎn)不會(huì)從通知一欄移除
mNM.cancel(id);
}
}
經(jīng)測(cè)試,360手機(jī)助手,騰訊手機(jī)管家都不能kill這個(gè)service,但是手動(dòng)結(jié)束后,再次打開發(fā)現(xiàn)音頻還在播放(跟音頻有關(guān)的客戶端),感覺有點(diǎn)小別扭
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android應(yīng)用開發(fā)SharedPreferences存儲(chǔ)數(shù)據(jù)的使用方法
- Android 動(dòng)畫之TranslateAnimation應(yīng)用詳解
- Android 動(dòng)畫之ScaleAnimation應(yīng)用詳解
- Android開發(fā)中多進(jìn)程共享數(shù)據(jù)簡(jiǎn)析
- Android 多進(jìn)程資料總結(jié)
- Android 進(jìn)程間通信實(shí)現(xiàn)原理分析
- Android應(yīng)用程序四大組件之使用AIDL如何實(shí)現(xiàn)跨進(jìn)程調(diào)用Service
- 解析后臺(tái)進(jìn)程對(duì)Android性能影響的詳解
- Android結(jié)束進(jìn)程的方法詳解
- Android中應(yīng)用多進(jìn)程的整理總結(jié)
相關(guān)文章
詳解Android studio如何導(dǎo)入jar包方法
這篇內(nèi)容主要給大家詳細(xì)說明了如何導(dǎo)入jar包,以及Android studio遇到的各種問題和解決辦法。2017-12-12
Android仿淘寶首頁頭條View垂直滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android仿淘寶首頁頭條View垂直滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
android ListView和ProgressBar(進(jìn)度條控件)的使用方法
這篇文章主要介紹了android ListView控件的使用方法和ProgressBar(進(jìn)度條控件)的使用方法,代碼大家可以參考使用2013-11-11
Android實(shí)現(xiàn)邊錄邊播應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)邊錄邊播應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Android實(shí)現(xiàn)簡(jiǎn)易的鬧鐘功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)易的鬧鐘功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
Kotlin RadioGroup與ViewPager實(shí)現(xiàn)底層分頁按鈕方法
安卓的控件是挺多的,沒有辦法一個(gè)一個(gè)的來說明,我們挑出了一些重點(diǎn)的控件,組成一些常見的布局,這樣以后在遇到相同功能的界面時(shí),就會(huì)有自己的思路,或者進(jìn)行復(fù)用2022-12-12

