您的位置:首页 > 移动开发 > Android开发

android获取用户当前正在打开的应用程序的信息

2012-08-27 09:01 627 查看
需要用到ActivityManager.
getRunningTasks(int maxNum)

public     List<ActivityManager.RunningTaskInfo>     
getRunningTasks(int maxNum)

API介绍说:

Return a list of the tasks that are currently running, with the most recent being first and older ones after in order.  Note that "running" does not mean any of the task's code is currently loaded or activity -- the task may
have been frozen by the system, so that it can be restarted in its previous state when next brought to the foreground.

maxNum          The maximum number of entries to return in the list.  The actual number returned may be smaller, depending on how many tasks the user has started.
当我们把1传递给maxNum时,返回的就是当前运行的那个TASK,然后就可以从task中获取最顶层的activity,此activity就是当前显示给用户那个activity。

1
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); 
2
ComponentName cn = am.getRunningTasks(
1
).get(
0
).topActivity; 
3
Log.d(
""
,
"pkg:"
+cn.getPackageName()); 
4
Log.d(
""
,
"cls:"
+cn.getClassName());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐