forceStopPackage与killBackgroundProcesses方法
最近了解一键清理功能,需要实现强制关闭进程的功能。下面介绍下killBackgroundProcesses()方法和forceStopPackage()方法。
killBackgroundProcesses()
ActivityManager的killBackgroundProcesses方法,可以立即杀死与指定包相关联的所有后台进程,这与内核杀死那些进程回收内存是一样的,但这些进程如果在将来某一时刻需要使用,会重新启动。该方法需要权限android.permission.KILL_BACKGROUND_PROCESSES。源码解释如下:
- /**
- * 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) {
- }
- }
forceStopPackage()
调用此方法,系统会强制停止与指定包关联的所有事情,将会杀死使用同一个uid的所有进程,停止所有服务,移除所有activity。所有注册的定时器和通知也会移除。forceStopPackage方法源码解释如下:
- /**
- * 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.
- * @param userId The user for which the running package is 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 forceStopPackageAsUser(String packageName, int userId) {
- try {
- ActivityManagerNative.getDefault().forceStopPackage(packageName, userId);
- } catch (RemoteException e) {
- }
- }
- /**
- * @see #forceStopPackageAsUser(String, int)
- * @hide
- */
- public void forceStopPackage(String packageName) {
- forceStopPackageAsUser(packageName, UserHandle.myUserId());
- }
注意使用forceStopPackage方法时,需要添加权限android.permission.FORCE_STOP_PACKAGES。同时注意该方法是隐藏的方法,需要使用反射机制调用。如下:
- ActivityManager mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
- Method method = Class.forName("android.app.ActivityManager").getMethod("forceStopPackage", String.class);
- method.invoke(mActivityManager, packageName); //packageName是需要强制停止的应用程序包名
forceStopPackage与killBackgroundProcesses方法的更多相关文章
- Android ActivityManager.killBackgroundProcesses方法去结束
android2.2以后,如果服务在ondestroy里加上了start自己,用kill backgroudprocess通常无法结束自己.有一种最新发现的方法,利用反射调用forceStopPack ...
- android结束进程、退出application的方法
1.finish()方法 finish是Activity的类,仅仅针对Activity,当调用finish()时,只是将活动推向后台,并没有立即释放内存,活动的资源并没有被清理:调用finish()方 ...
- Android 中退出程序的几种方法
1.finish()方法 finish是Activity的类,仅仅针对Activity,当调用finish()时,只是将活动推向后台,并没有立即释放内存,活动的资源并没有被清理:调用finish()方 ...
- Android杀死进程方法
1. android.os.Process.killProcess(pid) 只能终止本程序的进程,无法终止其它的 具体代码如下: ?12 Process.killProcess(Process.my ...
- [转]Android进程与线程基本知识
转自:http://www.cnblogs.com/hanyonglu/archive/2012/04/12/2443262.html 本文介绍Android平台中进程与线程的基本知识. 很早的时候就 ...
- Android adb 命令
一.概述 作为一名开发者,相信对adb指令一定不会陌生.那么在手机连接adb后,可通过am命令做很多操作: (1) 拨打电话10086 adb shell am start -a android.in ...
- java中的反射机制在Android开发中的用处
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反 ...
- [转]uses-permission权限列表
android.permission.ACCESS_CHECKIN_PROPERTIES允许读写访问”properties”表在checkin数据库中,改值可以修改上传 android.permiss ...
- uses-permission权限列表
android.permission.ACCESS_CHECKIN_PROPERTIES允许读写访问”properties”表在checkin数据库中,改值可以修改上传 android.permiss ...
随机推荐
- dbeaver能执行存储过程,db2命令编辑器里面不行
- CentOS 6, 编译安装lamp (php-fpm)
1 整体要求 php-fpm.httpd.mysql三者分别安装在三台虚拟机上: 第一台虚拟主机用于安装Mariadb,第二台虚拟主机安装php-fpm:第三台虚拟主机安装httpd.三台主机安装完之 ...
- 编译Nginx, 并使用自签证书实现https访问
1. 编译安装nginx1.8.1 [root@centos7 nginx-1.8.1]# ./configure --prefix=/usr/local/nginx.1.8.1 --with-htt ...
- 树莓派 -- 输入设备驱动 (key) 续2: 转载 Setting up a GPIO-Button “keyboard” on a Raspberry Pi
使用device-tree (DT) overlay应该是更方便的方法: http://blog.gegg.us/2017/01/setting-up-a-gpio-button-keyboard-o ...
- LINUX系统---初级相关操作和知识
LINUX系统的初级,从安装LINUX开始,到处理简单的运维问题.搭建各种服务.解决网路问题.缓解服务器压力,写简单的shell脚本. 我们从基本的入门开始搞事情: 安装LINUX系统 对磁盘的使用 ...
- 集训第四周(高效算法设计)L题 (背包贪心)
Description John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of ...
- vscode调试nodejs
1.安装nodejs 2.安装vscode 3.vscode安装debugger for chrome插件 4.新建nodejs-test文件夹,新建server.js空白文件,添加内容: var h ...
- jQuery_计算器实例
知识点: fadeIn()---计算器界面载入淡入效果 hover()---鼠标移入移出某个元素时触发的事件 click()---鼠标单击事件 css()---对元素样式的操作 val()---获取表 ...
- Spark 静态内存管理
作者编辑:杜晓蝶,王玮,任泽 Spark 静态内存管理详解 一. 内容简介 spark从1.6开始引入了动态内存管理模式,即执行内存和存储内存之间可以互相抢占.spark提供两种内存分配模式,即:静态 ...
- 转载:C/C++检测内存泄漏的工具 vld Visual Leak Detector223 的使用方法和sample示例
这类的工具有 比如 :LeakDiag leakfinder "Visual Leak Detector" vld可以从http://vld.codeplex.com/releas ...