由于一些需求的原因需要让自己App长时间在后台。虽然这样的做法是很Orz的,但是由于项目需求有时候不得不这样做。QQ、微信之所以没被第三方应用直接给kill掉,从市场原因腾讯的软件已经深入人心,很多厂家或是软件早就把这些软件加入白名单中!

  但是作为App的开发人员来说肯定是强烈建议不要这么做,不仅仅从用户角度考虑,作为Android开发者也有责任去维护Android的生态环境。就是因为越来越多的这样的占用系统内存的进程越来越多才使得android手机越来越卡,android碎片化的原因越来越多的第三方App可以通过清理内存的方式将后台进程给直接Kill掉来增加run可用内存。

  但是通过技术层面如何解决这个不被kill掉进程的问题呢?

  • Service设置成START_STICKY,kill 后会被重启(等待5秒左右),重传Intent,保持与重启前一样。(PM1中提供参考)
  • ​通过 startForeground将进程设置为前台进程,做前台服务,优先级和前台应用一个级别​,除非在系统内存非常缺,否则此进程不会被 kill
  • 双进程Service:让2个进程互相保护,其中一个Service被清理后,另外没被清理的进程可以立即重启进程
  • QQ黑科技:在应用退到后台后,另起一个只有 1 像素的页面停留在桌面上,让自己保持前台状态,保护自己不被后台清理工具杀死
  • 在已经root的设备下,修改相应的权限文件,将App伪装成系统级的应用(Android4.0系列的一个漏洞,已经确认可行)
  • Android系统中当前进程(Process)fork出来的子进程,被系统认为是两个不同的进程。当父进程被杀死的时候,子进程仍然可以存活,并不受影响。鉴于目前提到的在Android-Service层做双守护都会失败,我们可以fork出c进程,多进程守护。死循环在那检查是否还存在,具体的思路如下(Android5.0以下可行)
  1. 用C编写守护进程(即子进程),守护进程做的事情就是循环检查目标进程是否存在,不存在则启动它。
  2. 在NDK环境中将1中编写的C代码编译打包成可执行文件(BUILD_EXECUTABLE)。
  3. 主进程启动时将守护进程放入私有目录下,赋予可执行权限,启动它即可。
  • 联系厂商,加入白名单

  PM1:

在基类service的oncreate中定义了一个notification,重要属性:
notification.flags = Notification.FLAG_NO_CLEAR|Notification.FLAG_ONGOING_EVENT;然后startForeground(setClass().hashCode(), notification);使得服务能挂在通知栏,感谢1、3楼提供的思路。接着我监听了一个系统广播Intent.ACTION_TIME_TICK,这个广播每分钟发送一次,我们可以每分钟检查一次Service的运行状态,如果已经被结束了,就重新启动Service。关键代码:
boolean isServiceRunning = false;
ActivityManager manager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
  for (RunningServiceInfo service :manager.getRunningServices(Integer.MAX_VALUE)) {
    if("com.XXX.XXX.XXXService".equals(service.service.getClassName())){
    isServiceRunning = true;
    }
  }
  if (!isServiceRunning ) {
 Intent i = new Intent(context, com.XXX.XXX.XXXService.class);
  i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  context.startService(i);
  } 
  
PM2:

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);

  后来一次 做自定义Notification的时候,通知栏没有显示通知,查看后发现 service 也没被kill 。所以就进一步去研究了下 最后发现 只用两行代码就能保持服务不会被kill,并且不会有通知栏通知代码如下:

1
2
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);
 }
}
  除了加入白名单这种官方解决方法,谷歌提出了使用:ForegroundService 前台服务,让服务一直以前台任务的方式运行,可以在service 的oncreate来实现前台服务, 通过这个方法必须发送一个通知栏,让用户知道服务在运行。关于前台服务将在下一篇随笔中记录。
 

通过查询资料有人提到这个program:: droidwolf/NativeSubprocess · GitHub
  创建 linux 子进程的 so 库,当初用在 service 免杀上,经测试,在大部分机子上有用。NativeSubprocess 是一个可以让你在 android 程序中创建 linux 子进程并执行你的 java 代码的 so 库。由于市面上典型的内存清理工具值清理 apk 包关联的进程,而不会处理 linux 原生进程。回头试试看这样是否好使。

参考文章:http://www.jb51.net/article/73976.htm

  最后的最后还是要说:请不要做流氓软件!!让本来就碎片化的Android手机能运行的更顺畅。

关于防止App被第三方应用Kill掉的问题的更多相关文章

  1. Android: HowTo设置app不被系统kill掉

    有一种方法可以设置app永远不会被kill,AndroidManifest.xml 中添加: android:persistent="true" 适用于放在/system/app下 ...

  2. Android 保持Service不被Kill掉的方法--双Service守护 && Android实现双进程守护

    本文分为两个部分,第一部分为双Service守护,第二部分为双进程守护 第一部分: 一.Service简介:Java.lang.Object ↳Android.content.Context  ↳an ...

  3. Android下写一个永远不会被KILL掉的进程/服务

    Android 系统对于内存管理有自己的一套方法,为了保障系统有序稳定的运信,系统内部会自动分配,控制程序的内存使用.当系统觉得当前的资源非常有限的时候,为了保证一些优先级高的程序能运行,就会杀掉一些 ...

  4. 保持Service不被Kill掉的方法--双Service守护 && Android实现双进程守护

    本文分为两个部分,第一部分为双Service守护,第二部分为双进程守护 第一部分: 一.Service简介:Java.lang.Object ↳Android.content.Context  ↳an ...

  5. 【代码】Android: 怎样设置app不被系统k掉

    有一种方法可以设置app永远不会被kill,AndroidManifest.xml 中添加: android:persistent="true" 适用于放在/system/app下 ...

  6. mapreduce作业reduce被大量kill掉

    之前有一段时间.我们的hadoop2.4集群压力非常大.导致提交的job出现大量的reduce被kill掉.同样的job执行时间比在hadoop0.20.203上面长了非常多.这个问题事实上是redu ...

  7. 防止Android程序被系统kill掉的处理方法

    转载请注明出处:http://blog.csdn.net/cuiran/article/details/38851401 目前遇到一个问题程序需要一直运行,并显示在最前端,但是运行一段时间发现会被系统 ...

  8. ORACLE快速彻底Kill掉的会话

    在ORACLE数据库当中,有时候会使用ALTER SYSTEM KILL SESSION 'sid,serial#'杀掉一个会话进程,但是使用这个SQL语句杀掉会话后,数据库并不会立即释放掉相关的资源 ...

  9. windows系统下在dos命令行kill掉被占用的pid (转)

    原文出自:http://www.2cto.com/os/201304/203771.html   windows系统下在dos命令行kill掉被占用的pid   1.开始-->运行-->c ...

随机推荐

  1. $_SERVER 的用法

    PHP编程中经常需要用到一些服务器的一些资料,特把$_SERVER的详细参数整理下,方便以后使用. $_SERVER['PHP_SELF'] #当前正在执行脚本的文件名,与 document root ...

  2. nginx:413 Request Entity Too Large 及 修改 PHP上传文件大小配置

    开发环境:CentOS + Nginx + PHP + MySql + phpMyAdmin 在用 phpMyAdmin 进行 sql 数据库导入的时候,经常需要上传比较大的 sql 数据文件,而这时 ...

  3. PHP获取当前页面的URL

    /** * 获取当前页面完整URL地址 * * @author 52php.cnblogs.com */ function http_get_page_url() { global $_G; if ( ...

  4. redis中的key设置过期时间

    EXPIRE key seconds 为给定  key  设置生存时间,当  key  过期时(生存时间为  0  ),它会被自动删除. 在 Redis 中,带有生存时间的  key  被称为『易失的 ...

  5. .net 根据银行卡获取银行信息

    using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary ...

  6. @echo off 与echo off

    echo off 关闭其他命令的显示(但是不包括自身这条命令) @echo off 关闭所有命令的显示(@关闭其后命令的显示)

  7. (备忘)Rect和RectF的区别

    1.Rect的变量使用int类型,而RectF使用float类型. 2.一些方法区别 <1>.Rect类 equals(Object obj) (for some reason it as ...

  8. 使用http.sys,让delphi 的多层服务飞起来

    核心提示:一直以来,delphi 的网络通讯层都是以indy 为主,虽然indy 的功能非常多,涉及到网络服务的各个方面,但是对于大多数多层服务来说,就是需要一个快速.稳定.高效的传输层.Delphi ...

  9. CTR预估评价指标介绍

    1 离线指标 1.1 LogLoss 1.1.1 KL散度 logloss使用KL散度来计算.设样本的真实分布为P,预测分布为Q,则KL散度定义如下: 这里可以通俗地把KL散度理解为相同事件空间里两个 ...

  10. sharepoint OWA问题解决

    请检查DNS   检查域名解析   cmd----nslookup---输入IP或者域名(计算机名)可以看到