最近了解一键清理功能,需要实现强制关闭进程的功能。下面介绍下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. [kuangbin带你飞]专题四 最短路练习

    对于最短路,我主要使用的就是dijkstra,Floyd,SPFA这三个算法.先来介绍一下这三个算法. 1. dijkstra算法.它适用于边权为正的情况,它是单源最短路,就是从单个源点出发到所有的结 ...

  2. [Python3网络爬虫开发实战] 3.3-正则表达式

    本节中,我们看一下正则表达式的相关用法.正则表达式是处理字符串的强大工具,它有自己特定的语法结构,有了它,实现字符串的检索.替换.匹配验证都不在话下. 当然,对于爬虫来说,有了它,从HTML里提取想要 ...

  3. 当执行计划中出现BITMAP CONVERSION TO ROWIDS关键字时,需要注意了。

    前言 前些天优化了一些耗费buffers较多的SQL,但系统CPU降低的效果不明显,于是又拉了awr报告,查看了SQL ordered by Gets排名前列的SQL. 分析 SQL代码: selec ...

  4. Linux命令学习(6):paste合并几列文件

    如果我们有三个文件: $ cat name.txt #姓名文档 Kevin Mary Tom $ cat gender.txt #性别文档 M F M $ cat age.txt #年龄文档 我们想把 ...

  5. python 类的装饰器

    我们知道,在不改变原有代码的基础上,我们可以使用装饰器为函数添加新的功能.同理,一切皆对象,我们也可以使用装饰器为类添加类属性.what? def deco(obj): obj.x = 1 obj.y ...

  6. 指定PING的网卡

    struct ifreq ifr; // 绑定在eth0上 memset( &ifr, , sizeof( struct ifreq ) ); snprintf( ifr.ifr_name, ...

  7. LeetCode 123. Best Time to Buy and Sell Stock III (stock problem)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  8. 九度oj 题目1182:统计单词

    题目1182:统计单词 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4780 解决:1764 题目描述: 编一个程序,读入用户输入的,以“.”结尾的一行文字,统计一共有多少个单词,并分别 ...

  9. 九度oj 题目1060:完数VS盈数

    题目1060:完数VS盈数 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6461 解决:2426 题目描述: 一个数如果恰好等于它的各因子(该数本身除外)子和,如:6=3+2+1.则称其 ...

  10. 建造高塔(codevs 1689)

    题目描述 Description n有n种石块,石块能无限供应.每种石块都是长方体,其中第i种石块的长.宽.高分别为li.wi.hi.石块可以旋转,使得其中两维成为长度和宽度,第三维成为高度.如果要把 ...