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.
当我们把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();
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. |
---|
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();
相关文章推荐
- android 如何 获取当前『前台』正在运行的程序名?
- Android获取当前正在运行的线程和使用的内存
- Android中LocationManager的简单使用,获取当前位置
- Android中LocationManager的简单使用,获取当前位置[转
- android中获取当前程序路径
- Android中TASK和PROCESS的区别,获取最近运行的程序和正在运行的进
- Android中获取正在运行的服务-------ActivityManager.RunningServiceInfo的使用(三)
- Android中获取正在运行的服务-------ActivityManager.RunningServiceInfo的使用
- Android获取正在运行的程序并kill掉它
- Android中TASK和PROCESS的区别,获取最近运行的程序和正在运行的进程
- 使用GetModuleFileName函数获取当前程序所在目录
- Android中获取正在运行的服务:ActivityManager.RunningServiceInfo的使用
- 在Linux下获取当前运行程序可执行文件全路径(不使用real_path()函数)
- Android 获取正在使用的运行时
- 获取当前使用某个TCODE的用户的程序样例
- 获取当前使用Android的用户的Id
- Android:分析app、Process、Task之间的区别,以及获取最近运行的程序(近期任务)与正在运行的程序
- Android 成功 使用GPS获取当前地理位置(解决getLastKnownLocation 返回 null)
- cocos2d-x获取程序当前内存使用量 GetProcessMemoryInfo 函数
- android获取当前正在运行的栈顶的应用包名