最近在做内存加速性质的APP,发现用killBackgroundProcesses()函数来杀一些进程总是杀不死。
经人提醒得知,不同进程的程序可能有相同的UID:比如微信,一般而言,微信有两个进程在跑:
com.tencent.mm 和 com.tencent.mm:push
无论你单独杀死哪个进程,另外一个进程在监测到兄弟进程被杀死后,就会立即启动被杀死的进程。
所以单独杀死其中一个是无用的,解决的思路就是:将拥有同一个UID的进程全部杀死。
后来补充:
通过查看ActivityManager的源码发现,真正杀不死的原因是下面这个解释:
系统会在需要的时候再次重启被我们杀死的进程。
真正彻底杀死系统进程的方法是:
而该方法是@hide , 并且,注释也说明得很清楚了:第三方应用不能调用该方法。
如果你想在自己的第三方app中使用该方法,那是一件很困难的事情,你不仅得获取一个操作系统的签名,还得把自己的应用设成
android:sharedUserId="android.uid.system" 权限。
这种情况作为第三方应用无疑是不可能实现的。由于该函数的功能是如此强大:该函数会停止所有和该程序包相关的:同uid程序、相关services、相关Activity等。
这也应该可以看成是Android的一种安全机制。如果第三方应用随便可以调用该函数,那么第三方应用就可以肆意来阻止竞品的程序启动以及其他破坏性工作。
一般运用这个方法来查杀进程的基本都是操作系统本身来实现靠谱一些,比如小米的MIUI,阿里的YunOS,魅族的Flyme等。