转载自Kitty_Landon forceStopPackage与killBackgroundProcesses方法

最近了解一键清理功能,需要实现强制关闭进程的功能。下面介绍下killBackgroundProcesses()方法和forceStopPackage()方法。

killBackgroundProcesses()

ActivityManager的killBackgroundProcesses方法,可以立即杀死与指定包相关联的所有后台进程,这与内核杀死那些进程回收内存是一样的,但这些进程如果在将来某一时刻需要使用,会重新启动。该方法需要权限Android.permission.KILL_BACKGROUND_PROCESSES。源码解释如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * Have the system immediately kill all background processes associated 
  3.  * with the given package.  This is the same as the kernel killing those 
  4.  * processes to reclaim memory; the system will take care of restarting 
  5.  * these processes in the future as needed. 
  6.  * 
  7.  * <p>You must hold the permission 
  8.  * {@link android.Manifest.permission#KILL_BACKGROUND_PROCESSES} to be able to 
  9.  * call this method. 
  10.  * 
  11.  * @param packageName The name of the package whose processes are to 
  12.  * be killed. 
  13.  */  
  14. public void killBackgroundProcesses(String packageName) {  
  15.     try {  
  16.         ActivityManagerNative.getDefault().killBackgroundProcesses(packageName,  
  17.                 UserHandle.myUserId());  
  18.     } catch (RemoteException e) {  
  19.     }  
  20. }  

forceStopPackage()

调用此方法,系统会强制停止与指定包关联的所有事情,将会杀死使用同一个uid的所有进程,停止所有服务,移除所有activity。所有注册的定时器和通知也会移除。forceStopPackage方法源码解释如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * Have the system perform a force stop of everything associated with 
  3.  * the given application package.  All processes that share its uid 
  4.  * will be killed, all services it has running stopped, all activities 
  5.  * removed, etc.  In addition, a {@link Intent#ACTION_PACKAGE_RESTARTED} 
  6.  * broadcast will be sent, so that any of its registered alarms can 
  7.  * be stopped, notifications removed, etc. 
  8.  * 
  9.  * <p>You must hold the permission 
  10.  * {@link android.Manifest.permission#FORCE_STOP_PACKAGES} to be able to 
  11.  * call this method. 
  12.  * 
  13.  * @param packageName The name of the package to be stopped. 
  14.  * @param userId The user for which the running package is to be stopped. 
  15.  * 
  16.  * @hide This is not available to third party applications due to 
  17.  * it allowing them to break other applications by stopping their 
  18.  * services, removing their alarms, etc. 
  19.  */  
  20. public void forceStopPackageAsUser(String packageName, int userId) {  
  21.     try {  
  22.         ActivityManagerNative.getDefault().forceStopPackage(packageName, userId);  
  23.     } catch (RemoteException e) {  
  24.     }  
  25. }  
  26.   
  27. /** 
  28.  * @see #forceStopPackageAsUser(String, int) 
  29.  * @hide 
  30.  */  
  31. public void forceStopPackage(String packageName) {  
  32.     forceStopPackageAsUser(packageName, UserHandle.myUserId());  
  33. }  

注意使用forceStopPackage方法时,需要添加权限android.permission.FORCE_STOP_PACKAGES。同时注意该方法是隐藏的方法,需要使用反射机制调用。如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ActivityManager mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);  
  2. Method method = Class.forName("android.app.ActivityManager").getMethod("forceStopPackage", String.class);  
  3. method.invoke(mActivityManager, packageName);  //packageName是需要强制停止的应用程序包名  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值