Android编程防止进程被第三方软件杀死的方法
作者:cc1218 发布时间:2021-08-01 01:52:46
标签:Android,进程
本文实例讲述了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程序设计有所帮助。


猜你喜欢
- 使用WPF做的一个简单的操作文件的demo,包括复制和移动文件夹,核心思想就是使用递归,如果只是移动或者复制单一文件,直接使用File.Co
- 前言在分布式场景下为了保证数据最终一致性。在单进程的系统中,存在多个线程可以同时改变某个变量(可变共享变量)时,就需要对变量或代码块做同步(
- 1. A SOAP 1.2 message is not valid when sent to a SOAP 1.1 only endpoi
- 详细描述maven中央仓库发布jar包的中间过程, 以及遇到的一些问题汇总, 尽量用文字描述清楚, 耐心看下去, 就一定会发布成功----S
- 前言最近接手的项目里涉及到了 GIF 动图的播放与监听,在上一版本中对于 GIF 的处理是由 H5 来实现的,因为考虑到用户体验,因此现在的
- 原理:创建一个新的Bitmap,然后再根据它来创建一个Canvas,最后调用View的draw方法将View画到Canvas上,这样得到的B
- 先来看看网易云APP的效果:前言关于网易云音乐推荐歌单界面的实现一、实现1.自定义一个圆角图片控件(也可直接使用第三方框架)由于是一些简单的
- Android ListView的Item点击效果的定制
- 提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档前言这两天在项目中使用到Java的导入导出功能,以前对这块有一定了解,但是没
- 一、前端搭建1、前端用到js:uploadify(下载地址:http://www.uploadify.com/download/)、laye
- 本文实例为大家分享了C#串口通信工具类的封装代码,供大家参考,具体内容如下 1、SerialPortHelper串口工具类封装us
- 起源 [1946: John von Neumann, Stan Ulam, and Nick Metropolis, all a
- 1、在java的构造方法中提供了 异常链.. 也就是我们可以通过构造方法不断的将 异常串联成一个异常链... 之所以需
- Javaweb分页技术实现分页技术就是通过SQL语句(如下)来获取数据,具体实现看下面代码//分页查询语句select * from 表名
- 本文实例为大家分享了Unity实现物体左右移动效果的具体代码,供大家参考,具体内容如下效果如下代码:using UnityEngine;us
- 如何打印GC日志排查问题在工作当中,有时候我们会需要打印GC的相关信息来定位问题。该如何做呢?先来看个示例public static voi
- @ModelAttribute在父类、子类的执行顺序被 @ModelAttribute 注解的方法会在Controller每个方法执行之前都
- Spring中BeanFactory FactoryBean和ObjectFactory的三种的区别引言关于FactoryBean 和 Be
- 1.数据类型的分类Java的数据类型主要分为两类:基本数据类型、引用数据类型Java中的字符串String属于引用数据类型。因为String
- Android 应用签名的两种方法一、使用pem签名 (一) apk签名命令java –jar sign