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)在還沒(méi)弄明白)。使用小米系統(tǒng)的進(jìn)程管理優(yōu)化內(nèi)存就不報(bào)錯(cuò)。
后來(lái)想到用Service防止軟件被kill掉,查了下資料,發(fā)現(xiàn)google 管方就有,F(xiàn)oregroundService 前臺(tái)服務(wù),讓服務(wù)一直以前臺(tái)任務(wù)的方式運(yùn)行,可以在service 的oncreate來(lái)實(shí)現(xiàn)前臺(tái)服務(wù), 通過(guò)這個(gè)方法必須發(fā)送一個(gè)通知欄,讓用戶(hù)知道服務(wù)在運(yùn)行。
Notification notification = new Notification(R.drawable.icon, "服務(wù)開(kāi)啟", 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。
后來(lái)一次 做自定義Notification的時(shí)候,通知欄沒(méi)有顯示通知,查看后發(fā)現(xiàn) service 也沒(méi)被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); } // 以兼容性方式開(kāi)始前臺(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é)束后,再次打開(kāi)發(fā)現(xiàn)音頻還在播放(跟音頻有關(guān)的客戶(hù)端),感覺(jué)有點(diǎn)小別扭
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android應(yīng)用開(kāi)發(fā)SharedPreferences存儲(chǔ)數(shù)據(jù)的使用方法
- Android 動(dòng)畫(huà)之TranslateAnimation應(yīng)用詳解
- Android 動(dòng)畫(huà)之ScaleAnimation應(yīng)用詳解
- Android開(kāi)發(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編程實(shí)現(xiàn)仿QQ發(fā)表說(shuō)說(shuō),上傳照片及彈出框效果【附demo源碼下載】
這篇文章主要介紹了Android編程實(shí)現(xiàn)仿QQ發(fā)表說(shuō)說(shuō),上傳照片及彈出框效果,涉及Android動(dòng)畫(huà)特效的相關(guān)實(shí)現(xiàn)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-01-01Android Jetpack組件庫(kù)LiveData源碼深入探究
LiveData是Jetpack組件的一部分,更多的時(shí)候是搭配ViewModel來(lái)使用,相對(duì)于Observable,LiveData的最大優(yōu)勢(shì)是其具有生命感知的,換句話(huà)說(shuō),LiveData可以保證只有在組件( Activity、Fragment、Service)處于活動(dòng)生命周期狀態(tài)的時(shí)候才會(huì)更新數(shù)據(jù)2022-09-09android viewpager實(shí)現(xiàn)豎直滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了android viewpager實(shí)現(xiàn)豎直滑動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07android 手機(jī)SD卡讀寫(xiě)操作(以txt文本為例)實(shí)現(xiàn)步驟
要完成SD卡讀寫(xiě)操作首先對(duì)manifest注冊(cè)SD卡讀寫(xiě)權(quán)限其次是創(chuàng)建一個(gè)對(duì)SD卡中文件讀寫(xiě)的類(lèi)寫(xiě)一個(gè)用于檢測(cè)讀寫(xiě)功能的的布局然后就是UI的類(lèi)了,感興趣的朋友可以參考下,希望可以幫助到你2013-02-02Android Studio實(shí)現(xiàn)井字游戲
這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)井字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Android TextView的TextWatcher使用案例詳解
這篇文章主要介紹了Android TextView的TextWatcher使用案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Android 自定義圓形頭像CircleImageView支持加載網(wǎng)絡(luò)圖片的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 自定義圓形頭像CircleImageView支持加載網(wǎng)絡(luò)圖片的實(shí)現(xiàn)代碼,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10Android自定義Spinner下拉列表(使用ArrayAdapter和自定義Adapter實(shí)現(xiàn))
這篇文章主要介紹了Android自定義Spinner下拉列表(使用ArrayAdapter和自定義Adapter實(shí)現(xiàn))的相關(guān)資料,需要的朋友可以參考下2015-10-10Android Studio時(shí)間選擇器的創(chuàng)建方法
這篇文章主要為大家詳細(xì)介紹了Android Studio時(shí)間選擇器的創(chuàng)建方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10