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

所属分类: 软件编程 / Android 阅读数: 2088
收藏 0 赞 0 分享

本文实例讲述了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程序设计有所帮助。

更多精彩内容其他人还在看

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多