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

android 获取当前正在使用的程序名和activity类名

2013-03-05 09:40 405 查看
需要用到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

new Thread() {

@Override

public void run(){

while(true) {

ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

ComponentName cn = am.getRunningTasks(1).get(0).topActivity;

System.out.println("当前的使用的包名pkg:"+cn.getPackageName());

System.out.println("当前的使用的activity类名:"+cn.getClassName());

try {

Thread.sleep(2000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}.start();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: