关于killBackgroundProcesses()函数,杀不死进程的解释
2015-01-14 10:28
141 查看
最近在做内存加速性质的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等。
经人提醒得知,不同进程的程序可能有相同的UID:比如微信,一般而言,微信有两个进程在跑:
com.tencent.mm 和 com.tencent.mm:push
无论你单独杀死哪个进程,另外一个进程在监测到兄弟进程被杀死后,就会立即启动被杀死的进程。
所以单独杀死其中一个是无用的,解决的思路就是:将拥有同一个UID的进程全部杀死。
后来补充:
通过查看ActivityManager的源码发现,真正杀不死的原因是下面这个解释:
/** * Have the system immediately kill all background processes associated * with the given package. This is the same as the kernel killing those * processes to reclaim memory; the system will take care of restarting * these processes in the future as needed. * * <p>You must hold the permission * {@link android.Manifest.permission#KILL_BACKGROUND_PROCESSES} to be able to * call this method. * * @param packageName The name of the package whose processes are to * be killed. */ public void killBackgroundProcesses(String packageName) { try { ActivityManagerNative.getDefault().killBackgroundProcesses(packageName, UserHandle.myUserId()); } catch (RemoteException e) { } }
系统会在需要的时候再次重启被我们杀死的进程。
真正彻底杀死系统进程的方法是:
/** * Have the system perform a force stop of everything associated with * the given application package. All processes that share its uid * will be killed, all services it has running stopped, all activities * removed, etc. In addition, a {@link Intent#ACTION_PACKAGE_RESTARTED} * broadcast will be sent, so that any of its registered alarms can * be stopped, notifications removed, etc. * * <p>You must hold the permission * {@link android.Manifest.permission#FORCE_STOP_PACKAGES} to be able to * call this method. * * @param packageName The name of the package to be stopped. * * @hide This is not available to third party applications due to * it allowing them to break other applications by stopping their * services, removing their alarms, etc. */ public void forceStopPackage(String packageName) { try { ActivityManagerNative.getDefault().forceStopPackage(packageName, UserHandle.myUserId()); } catch (RemoteException e) { } }
而该方法是@hide , 并且,注释也说明得很清楚了:第三方应用不能调用该方法。
如果你想在自己的第三方app中使用该方法,那是一件很困难的事情,你不仅得获取一个操作系统的签名,还得把自己的应用设成
android:sharedUserId="android.uid.system" 权限。
这种情况作为第三方应用无疑是不可能实现的。由于该函数的功能是如此强大:该函数会停止所有和该程序包相关的:同uid程序、相关services、相关Activity等。
这也应该可以看成是Android的一种安全机制。如果第三方应用随便可以调用该函数,那么第三方应用就可以肆意来阻止竞品的程序启动以及其他破坏性工作。
一般运用这个方法来查杀进程的基本都是操作系统本身来实现靠谱一些,比如小米的MIUI,阿里的YunOS,魅族的Flyme等。
相关文章推荐
- Android之关于killBackgroundProcesses()函数,杀不死进程的解释
- Android中杀进程的几种方法 (1) - killBackgroundProcesses
- Android怎么结束一个进程,我试了用 ActivityManager.killBackgroundProcesses方法去结束,但是没有反应
- 一个关于linux进程函数kill的问题
- C++中的类所占内存空间总结(其中有一段关于成员函数处于代码段的解释) 2011-12-9 16:16
- 关于函数指针的解释
- 解释关于操作系统进程调度的误解(自己对它的误解)
- kill -9 无法结束进程 解释
- 一劳永逸:关于C/C++中指针、数组与函数复合定义形式的直观解释
- Oracle后台进程之:Oracle Mandatory Background Processes (PMON、SMON、LGWR、DBWN、CKPT、MMON、MMNL、RECO)
- 关于进程与线程:About Processes and Threads
- C语言信号传送函数kill()_传送信号给指定的进程
- 关于oracle初始化参数中db_writer_processes和dbwr_io_slaves含义的解释
- 关于两个小东东的分析(函数执行值与函数的预编译跟解释执行阶段)
- D u p l i c a t e H a n d l e函数存在的奇怪现象之一是,目标进程没有得到关于新内 核对象现在可以访问它的通知
- 关于C中函数参数,指针-常量-引用解释的4个函数例子
- 由CloseHandle()函数引发的关于线程和线程句柄的一些解释
- 几个关于进程的函数
- Linux中关于进程方面常用函数的区别
- 一劳永逸:关于C/C++中指针、数组与函数复合定义形式的直观解释