欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程防止進(jìn)程被第三方軟件殺死的方法

 更新時間:2015年10月27日 14:15:05   作者:cc1218  
這篇文章主要介紹了Android編程防止進(jìn)程被第三方軟件殺死的方法,涉及Android進(jìn)程操作的相關(guā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è)計有所幫助。

相關(guān)文章

最新評論