Android編程防止進(jìn)程被第三方軟件殺死的方法
本文實(shí)例講述了Android編程防止進(jìn)程被第三方軟件殺死的方法。分享給大家供大家參考,具體如下:
項(xiàng)目測試的時候發(fā)現(xiàn),按home鍵回到桌面,再用360清理內(nèi)存,軟件被結(jié)束,再次進(jìn)入的時候報錯,看了下log,以為是有的地方?jīng)]有控制好,但是又不知道360結(jié)束的是什么(這個現(xiàn)在還沒弄明白)。使用小米系統(tǒng)的進(jìn)程管理優(yōu)化內(nèi)存就不報錯。
后來想到用Service防止軟件被kill掉,查了下資料,發(fā)現(xiàn)google 管方就有,F(xiàn)oregroundService 前臺服務(wù),讓服務(wù)一直以前臺任務(wù)的方式運(yùn)行,可以在service 的oncreate來實(shí)現(xiàn)前臺服務(wù), 通過這個方法必須發(fā)送一個通知欄,讓用戶知道服務(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)行,可是通知欄不能清除 ,一清除就會被kill。
后來一次 做自定義Notification的時候,通知欄沒有顯示通知,查看后發(fā)現(xiàn) service 也沒被kill 。所以就進(jìn)一步去研究了下 最后發(fā)現(xiàn) 只用兩行代碼就能保持服務(wù)不會被kill,并且不會有通知欄通知代碼如下:
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)? // 前臺服務(wù)的 notification.flags 總是默認(rèn)包含了那個標(biāo)志位 Notification notification =new Notification(); // 注意使用 startForeground ,id 為 0 將不會顯示 notification startForegroundCompat(1, notification); } @Override public void onDestroy() { super.onDestroy(); stopForegroundCompat(1); } // 以兼容性方式開始前臺服務(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); } // 以兼容性方式停止前臺服務(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)槲覀冇锌赡茉谌∠芭_服務(wù)之后 // 的那一瞬間被kill掉。這個時候 notification 便永遠(yuǎn)不會從通知一欄移除 mNM.cancel(id); } }
經(jīng)測試,360手機(jī)助手,騰訊手機(jī)管家都不能kill這個service,但是手動結(jié)束后,再次打開發(fā)現(xiàn)音頻還在播放(跟音頻有關(guān)的客戶端),感覺有點(diǎn)小別扭
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android應(yīng)用開發(fā)SharedPreferences存儲數(shù)據(jù)的使用方法
- Android 動畫之TranslateAnimation應(yīng)用詳解
- Android 動畫之ScaleAnimation應(yīng)用詳解
- Android開發(fā)中多進(jìn)程共享數(shù)據(jù)簡析
- Android 多進(jìn)程資料總結(jié)
- Android 進(jìn)程間通信實(shí)現(xiàn)原理分析
- Android應(yīng)用程序四大組件之使用AIDL如何實(shí)現(xiàn)跨進(jìn)程調(diào)用Service
- 解析后臺進(jìn)程對Android性能影響的詳解
- Android結(jié)束進(jìn)程的方法詳解
- Android中應(yīng)用多進(jìn)程的整理總結(jié)
相關(guān)文章
Android編程實(shí)現(xiàn)仿QQ發(fā)表說說,上傳照片及彈出框效果【附demo源碼下載】
這篇文章主要介紹了Android編程實(shí)現(xiàn)仿QQ發(fā)表說說,上傳照片及彈出框效果,涉及Android動畫特效的相關(guān)實(shí)現(xiàn)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-01-01Android Jetpack組件庫LiveData源碼深入探究
LiveData是Jetpack組件的一部分,更多的時候是搭配ViewModel來使用,相對于Observable,LiveData的最大優(yōu)勢是其具有生命感知的,換句話說,LiveData可以保證只有在組件( Activity、Fragment、Service)處于活動生命周期狀態(tài)的時候才會更新數(shù)據(jù)2022-09-09android viewpager實(shí)現(xiàn)豎直滑動效果
這篇文章主要為大家詳細(xì)介紹了android viewpager實(shí)現(xiàn)豎直滑動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07android 手機(jī)SD卡讀寫操作(以txt文本為例)實(shí)現(xiàn)步驟
要完成SD卡讀寫操作首先對manifest注冊SD卡讀寫權(quán)限其次是創(chuàng)建一個對SD卡中文件讀寫的類寫一個用于檢測讀寫功能的的布局然后就是UI的類了,感興趣的朋友可以參考下,希望可以幫助到你2013-02-02Android Studio實(shí)現(xiàn)井字游戲
這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)井字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01Android TextView的TextWatcher使用案例詳解
這篇文章主要介紹了Android TextView的TextWatcher使用案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Android 自定義圓形頭像CircleImageView支持加載網(wǎng)絡(luò)圖片的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 自定義圓形頭像CircleImageView支持加載網(wǎng)絡(luò)圖片的實(shí)現(xiàn)代碼,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-10-10Android自定義Spinner下拉列表(使用ArrayAdapter和自定義Adapter實(shí)現(xiàn))
這篇文章主要介紹了Android自定義Spinner下拉列表(使用ArrayAdapter和自定義Adapter實(shí)現(xiàn))的相關(guān)資料,需要的朋友可以參考下2015-10-10Android Studio時間選擇器的創(chuàng)建方法
這篇文章主要為大家詳細(xì)介紹了Android Studio時間選擇器的創(chuàng)建方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10