当前位置: 移动技术网 > IT编程>移动开发>Android > Android编程防止进程被第三方软件杀死的方法

Android编程防止进程被第三方软件杀死的方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

湖北宜都,冬冬和七七,最强大脑中国vs西班牙

本文实例讲述了android编程防止进程被第三方软件杀死的方法。分享给大家供大家参考,具体如下:

项目测试的时候发现,按home键回到桌面,再用360清理内存,软件被结束,再次进入的时候报错,看了下log,以为是有的地方没有控制好,但是又不知道360结束的是什么(这个现在还没弄明白)。使用小米系统的进程管理优化内存就不报错。

后来想到用service防止软件被kill掉,查了下资料,发现google 管方就有,foregroundservice 前台服务,让服务一直以前台任务的方式运行,可以在service 的oncreate来实现前台服务, 通过这个方法必须发送一个通知栏,让用户知道服务在运行。

notification notification = new notification(r.drawable.icon, "服务开启", 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", "防止服务被任务管理器所杀", pendingintent);
startforeground(ongoing_notification, notification);

这样就能保持service 运行,可是通知栏不能清除 ,一清除就会被kill。
后来一次 做自定义notification的时候,通知栏没有显示通知,查看后发现 service 也没被kill 。所以就进一步去研究了下 最后发现 只用两行代码就能保持服务不会被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 设置 flag_ongoing_event,因为
  // 前台服务的 notification.flags 总是默认包含了那个标志位
  notification notification =new notification();
  // 注意使用 startforeground ,id 为 0 将不会显示 notification
  startforegroundcompat(1, notification);
 }
 @override
 public void ondestroy() {
  super.ondestroy();
  stopforegroundcompat(1);
 }
 // 以兼容性方式开始前台服务
 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);
 }
 // 以兼容性方式停止前台服务
 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 之前调用 cancel,因为我们有可能在取消前台服务之后
  // 的那一瞬间被kill掉。这个时候 notification 便永远不会从通知一栏移除
  mnm.cancel(id);
 }
}

经测试,360手机助手,腾讯手机管家都不能kill这个service,但是手动结束后,再次打开发现音频还在播放(跟音频有关的客户端),感觉有点小别扭

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网