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

killBackgroundProcesses()

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

  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方法源码解释如下:

  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. * @see #forceStopPackageAsUser(String, int)
  28. * @hide
  29. */
  30. public void forceStopPackage(String packageName) {
  31. forceStopPackageAsUser(packageName, UserHandle.myUserId());
  32. }

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

    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是需要强制停止的应用程序包名

forceStopPackage与killBackgroundProcesses方法的更多相关文章

  1. Android ActivityManager.killBackgroundProcesses方法去结束

    android2.2以后,如果服务在ondestroy里加上了start自己,用kill backgroudprocess通常无法结束自己.有一种最新发现的方法,利用反射调用forceStopPack ...

  2. android结束进程、退出application的方法

    1.finish()方法 finish是Activity的类,仅仅针对Activity,当调用finish()时,只是将活动推向后台,并没有立即释放内存,活动的资源并没有被清理:调用finish()方 ...

  3. Android 中退出程序的几种方法

    1.finish()方法 finish是Activity的类,仅仅针对Activity,当调用finish()时,只是将活动推向后台,并没有立即释放内存,活动的资源并没有被清理:调用finish()方 ...

  4. Android杀死进程方法

    1. android.os.Process.killProcess(pid) 只能终止本程序的进程,无法终止其它的 具体代码如下: ?12 Process.killProcess(Process.my ...

  5. [转]Android进程与线程基本知识

    转自:http://www.cnblogs.com/hanyonglu/archive/2012/04/12/2443262.html 本文介绍Android平台中进程与线程的基本知识. 很早的时候就 ...

  6. Android adb 命令

    一.概述 作为一名开发者,相信对adb指令一定不会陌生.那么在手机连接adb后,可通过am命令做很多操作: (1) 拨打电话10086 adb shell am start -a android.in ...

  7. java中的反射机制在Android开发中的用处

    JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反 ...

  8. [转]uses-permission权限列表

    android.permission.ACCESS_CHECKIN_PROPERTIES允许读写访问”properties”表在checkin数据库中,改值可以修改上传 android.permiss ...

  9. uses-permission权限列表

    android.permission.ACCESS_CHECKIN_PROPERTIES允许读写访问”properties”表在checkin数据库中,改值可以修改上传 android.permiss ...

随机推荐

  1. Ztree勾选节点后取消勾选其父子节点

    前言: Ztree官方给的API可以设置勾选一个节点的同时勾选子节点或者父节点,也可以设置不影响父子节点,即将chkboxType设置为{"Y":"",&quo ...

  2. PHP:图片上传

    文章来源:http://www.cnblogs.com/hello-tl/p/7593033.html <?php class TL_Update_File{ private $file = n ...

  3. 建仓类型与对应建仓价MT4

    建仓类型与对应建仓价 (Bid,Ask) 建仓类型 对应建仓价 Buy Ask+Spread Sell Bid-Spread BuyLimit Ask-Spread-StopLevel SellLim ...

  4. Android Studio 使用图片

    首先将图片放在drawable下 然后: <ImageView android:layout_width="wrap_content" android:layout_heig ...

  5. codeforces 359A

    #include<stdio.h> #define N 60 int map[N][N]; int main() {  int n,m,i,j,flag;  while(scanf(&qu ...

  6. jQuery插件之ajaxFileUpload(ajax文件上传)

    一.ajaxFileUpload是一个异步上传文件的jQuery插件. 传一个不知道什么版本的上来,以后不用到处找了. 语法:$.ajaxFileUpload([options]) options参数 ...

  7. Surprising Strings

    Surprising Strings Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description ...

  8. POJ 3268_Silver Cow Party

    题意: n个地方,标号1~n,每个地方都有一头牛,现在要他们都去往标号为x的地方,再从x返回,每条道路都是单向的,求所有牛走的来回的最短路中的最大值. 分析: 注意在求每头牛走到x时,挨个算肯定超时, ...

  9. HDU——1281 棋盘游戏

    棋盘游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  10. GNS3模拟的硬件

    Hardware emulated by GNS3 Cisco 1700 Series 1700s have one or more interfaces on the motherboard, 2 ...