ActivityManager
android.app.ActivityManager
这个类主要用来管理全部设备上的Activities。
权限:android.permission.GET_TASKS
方法:| 返回类型 方法|
1.List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, int flags)
返回用户近期使用过的应用程序信息集合。第一个參数是最大数量,第二个參数在API11前仅仅有ActivityManager.RECENT_WITH_EXCLUDED。这种方法以后可能会被废弃,谷歌推荐用来debug用。
(1) RecentTaskInfo类,主要有些字段
1.1 Intent baseIntent 得到能跳转到这个Task的Activity,即能通过这个Intent启动这个Intent指向的程序。
1.2 int id 得到这个Task的标识,假设是-1,则标识这个程序没启动,其它数字表示启动了。
1.3 int persistentId 任务的唯一值。
1.4 Intent baseIntent 启动任务的Intent
任务栏(长按Home键或者Menu键或者点击任务键)里面的任务主要就是靠这种方法获取的。
final List<ActivityManager.RecentTaskInfo> recentTasks =
am.getRecentTasks(MAX_TASKS, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
点击启动任务
if (ad.taskId >= 0) {
// This is an active task; it should just go to the foreground.
am.moveTaskToFront(ad.taskId, ActivityManager.MOVE_TASK_WITH_HOME,
opts);
} else {
Intent intent = ad.intent;
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
| Intent.FLAG_ACTIVITY_TASK_ON_HOME
| Intent.FLAG_ACTIVITY_NEW_TASK);
if (DEBUG) Log.v(TAG, "Starting activity " + intent);
context.startActivityAsUser(intent, opts,
new UserHandle(UserHandle.USER_CURRENT));
}
滑动删除任务(hide方法,需系统权限android.permission.REMOVE_TASKS)
am.removeTask(ad.persistentTaskId, ActivityManager.REMOVE_TASK_KILL_PROCESS);
设置中强行停止应用(hide,须要非public权限)
ActivityManager am = (ActivityManager)getActivity().getSystemService(
Context.ACTIVITY_SERVICE);
am.forceStopPackage(pkgName);
2.List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses()
返回设备上正在执行的程序的进程集合。
(1)RunningAppProcessInfo类,主要有些字段
1.1 int importance 这个字段的值假设为IMPORTANCE_FOREGROUND (100)时表示为前段执行的进程,为IMPORTANCE_BACKGROUND(400)表示后台执行。其值另一些其它的。
1.2 int importanceReasonCode 对进程进行解释的字段,有3个值 REASON_UNKNOWN(0) 、REASON_PROVIDER_IN_USE(1)、REASON_SERVICE_IN_USE(2)。分别表示没原因、程序中的content provider被其它程序使用、与前一个理由一样。
1.3 int importanceReasonPid 当 importanceReasonCode 的值不为0时这个字段代表1.2中其它程序的PID值,否则值为0。
1.4 ComponentName importanceReasonComponent 当 importanceReasonCode 中值不为0时,这个字段代表1.2中其它程序的 ComponentName,否则为null。
1.5 int pid 进程的PID值。
1.6 int uid user id.
1.7 String processName 进程名,实际等于包名(content provider免疫)。
1.8 String[] pkgList 程序内全部主包,这个測试出来,一般程序都仅仅有一个元素,但系统自带的程序而且有content provider的有几个包。
3 List<ActivityManager.RunningTaskInfo> getRunningTasks(int maxNum)
返回正在执行中的程序,參数为返回的最大个数,返回的顺序为 近期打开的程序,即优先返回最新使用的程序。返回值可能为空。
(1)RunningTaskInfo类,主要提供字段
1.1 ComponentName baseActivity 代表登陆的Activity的 ComponentName,即能够用这个返回值启动这个所代表的程序。
1.2 int id A unique identifier for this task.
1.3 int numActivities Number of activities in this task.
1.4 int numRunning Number of activities that are currently running (not stopped and persisted) in this task.
1.5 ComponentName topActivity 存在栈顶的Activity所代表的 ComponentName。用户能够用这种方法得到如今正在显示的Activity的 ComponentName。
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
4.void killBackgroundProcesses (String packageName)
杀死后台进程,參数为要被杀的程序的(主)包名。须要权限android.permission.KILL_BACKGROUND_PROCESSES。
5. List<ActivityManager.RunningServiceInfo> getRunningServices(int maxNum)
得到全部正在执行的service。
(1)RunningServiceInfo类,主要提供字段。
1.1 long activeSince 第一次启动这个service到如今所过的时间段。
1.2 int pid 进程的PID
1.3 int uid user id.
1.4 String process 进程名(包名?)。
1.5 long lastActivityTime 最后一次激活Service到如今的时间
1.6 ComponenName service 得到这个Service的组件名,就是能通过这个启动service
1.7 long restarting 这个字段的值假设不是0,那么如今这个service还没启动,将在返回值的时间段过后自启动。
6. void restartPackage (String packageName)
如今这种方法等于方法4了,调用这种方法等于在调用方法4.
ActivityManager的更多相关文章
- 使用ActivityManager实现进程管理
Android中使用ActivityManager可以获得进程信息,并对进程进行管理,如结束进程等.本文使用ActivityManager获得进程列表,并结束选中的进程. 首先,看看布局文件. < ...
- Android开发之ActivityManager获取系统信息
1.判断指定的service是否在运行 public static boolean isServiceRunning(Context ctx, String serviceName) { Activi ...
- ActivityManager: Warning: Activity not started, its current task has been brought to the front 的的问题
运行android程序的时候提示:ActivityManager: Warning: Activity not started, its current task has been brought t ...
- 出现错误ActivityManager: Warning: Activity not started, its current task has been
1.在学习两个Activity的切换时,重新把新的工程部署上模拟器时候出现错误:ActivityManager: Warning: Activity not started, its current ...
- Android中获取正在运行的应用程序-----ActivityManager.RunningAppProcessInfo类详解
今天继续讲解关于ActivityManager的使用,通过前面一节的学习,我们学会了如何利用ActivityManager获取系统里 正在运行的进程.本文要讲解的知识点是利用这些进程信息获取系统里正在 ...
- ActivityManager的使用
本节内容主要是讲解ActivityManager的使用,通过ActivityManager我们可以获得系统里正在运行的activities,包括 进程(Process)等.应用程序/包.服务(Serv ...
- Android ActivityManager.killBackgroundProcesses方法去结束
android2.2以后,如果服务在ondestroy里加上了start自己,用kill backgroudprocess通常无法结束自己.有一种最新发现的方法,利用反射调用forceStopPack ...
- Android编程之ActivityManager: Segmentation fault
今天运行代码时,出现了一个不能运行的故障问题:ActivityManager: Segmentation fault 是的,这个原因网上有诸多解释:包名不能是中文或者非法字符,或者重启新的avd来解决 ...
- Android源代码学习之六——ActivityManager框架解析
ActivityManager在操作系统中有关键的数据,本文利用操作系统源代码,逐步理清ActivityManager的框架,并从静态类结构图和动态序列图两个角度分别进行剖析,从而帮助开发者加强对系统 ...
随机推荐
- gzip解压压缩的字符串数据
import urllib2 from StringIO import StringIO import gzip def loadData(url): request = urllib2.Reques ...
- Flow Problem(最大流)
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- 如何提高banner设计含量--网上的一篇文章--感悟
"修改": 本质上是改什么?改大小?图片?文字?颜色? 老板说:修改本质上是提高“设计含量”.检测一个作品设计含量的高低,可以将作品中每一个设计元素进行分析,看它的“属性”与“操作 ...
- iReport5.6.0 linechart 制作方法
iReport 官网和文档上关于chart设计以饼图和JDBC源作为样例.但很多其它的情况下因为报表中的数据须要首先加工处理,因此很多其它的是从JavaBeans set datasource从获取数 ...
- 自己动手为PHP7添加新的语法特性
好文章! nikic介绍了如何向PHP添加新的语法特性,原文写的非常精彩,具体是添加in语法功能,使最终实现: <?php $words = ['hello', 'world', 'foo', ...
- UDP包结构
UDP包结构 // 参考: http://www.2cto.com/net/201307/224715.html UDP数据包由首部和数据组成,每行4个字节(32位),首部固定长度为8个字节(2行) ...
- 自定义UIViewController与xib文件关系深入分析
6月14日 上海 OSC 源创会开始报名啦,有很多机械键盘送哦!!! 用xcode模板向工程加入UIViewController sub class的时候,如果选中了with xib for inte ...
- 二维码类库--phpqrcode使用简介
#载入类文件 include 'phpqrcode.php'; $value = '二维码内容'; $errorCorrectionLevel = 'L';//容错级别 L.M.Q.H $matrix ...
- spring mvc 和ajax异步交互完整实例
Spring MVC 异步交互demo: 1.jsp页面: <%@ page language="java" contentType="text/html; cha ...
- OKR 方法 学习笔记
最近公司兴起了对OKR这个词的讨论,并且听到时总会伴随提到KPI,提到绩效考核.那OKR到底是什么呢?与KPI的区别在哪里?与绩效考核有什么关系?它与我们现在推行的敏捷开发有啥关系呢?因此,就到网上查 ...