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 ...
随机推荐
- Ztree勾选节点后取消勾选其父子节点
前言: Ztree官方给的API可以设置勾选一个节点的同时勾选子节点或者父节点,也可以设置不影响父子节点,即将chkboxType设置为{"Y":"",&quo ...
- PHP:图片上传
文章来源:http://www.cnblogs.com/hello-tl/p/7593033.html <?php class TL_Update_File{ private $file = n ...
- 建仓类型与对应建仓价MT4
建仓类型与对应建仓价 (Bid,Ask) 建仓类型 对应建仓价 Buy Ask+Spread Sell Bid-Spread BuyLimit Ask-Spread-StopLevel SellLim ...
- Android Studio 使用图片
首先将图片放在drawable下 然后: <ImageView android:layout_width="wrap_content" android:layout_heig ...
- codeforces 359A
#include<stdio.h> #define N 60 int map[N][N]; int main() { int n,m,i,j,flag; while(scanf(&qu ...
- jQuery插件之ajaxFileUpload(ajax文件上传)
一.ajaxFileUpload是一个异步上传文件的jQuery插件. 传一个不知道什么版本的上来,以后不用到处找了. 语法:$.ajaxFileUpload([options]) options参数 ...
- Surprising Strings
Surprising Strings Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description ...
- POJ 3268_Silver Cow Party
题意: n个地方,标号1~n,每个地方都有一头牛,现在要他们都去往标号为x的地方,再从x返回,每条道路都是单向的,求所有牛走的来回的最短路中的最大值. 分析: 注意在求每头牛走到x时,挨个算肯定超时, ...
- HDU——1281 棋盘游戏
棋盘游戏 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- GNS3模拟的硬件
Hardware emulated by GNS3 Cisco 1700 Series 1700s have one or more interfaces on the motherboard, 2 ...