最近了解一键清理功能,需要实现强制关闭进程的功能。下面介绍下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. dbeaver能执行存储过程,db2命令编辑器里面不行

  2. CentOS 6, 编译安装lamp (php-fpm)

    1 整体要求 php-fpm.httpd.mysql三者分别安装在三台虚拟机上: 第一台虚拟主机用于安装Mariadb,第二台虚拟主机安装php-fpm:第三台虚拟主机安装httpd.三台主机安装完之 ...

  3. 编译Nginx, 并使用自签证书实现https访问

    1. 编译安装nginx1.8.1 [root@centos7 nginx-1.8.1]# ./configure --prefix=/usr/local/nginx.1.8.1 --with-htt ...

  4. 树莓派 -- 输入设备驱动 (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 ...

  5. LINUX系统---初级相关操作和知识

    LINUX系统的初级,从安装LINUX开始,到处理简单的运维问题.搭建各种服务.解决网路问题.缓解服务器压力,写简单的shell脚本. 我们从基本的入门开始搞事情: 安装LINUX系统 对磁盘的使用 ...

  6. 集训第四周(高效算法设计)L题 (背包贪心)

    Description   John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of ...

  7. vscode调试nodejs

    1.安装nodejs 2.安装vscode 3.vscode安装debugger for chrome插件 4.新建nodejs-test文件夹,新建server.js空白文件,添加内容: var h ...

  8. jQuery_计算器实例

    知识点: fadeIn()---计算器界面载入淡入效果 hover()---鼠标移入移出某个元素时触发的事件 click()---鼠标单击事件 css()---对元素样式的操作 val()---获取表 ...

  9. Spark 静态内存管理

    作者编辑:杜晓蝶,王玮,任泽 Spark 静态内存管理详解 一. 内容简介 spark从1.6开始引入了动态内存管理模式,即执行内存和存储内存之间可以互相抢占.spark提供两种内存分配模式,即:静态 ...

  10. 转载:C/C++检测内存泄漏的工具 vld Visual Leak Detector223 的使用方法和sample示例

    这类的工具有 比如 :LeakDiag leakfinder "Visual Leak Detector" vld可以从http://vld.codeplex.com/releas ...