需要用到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()); |