getRunningTasks有个整型参数,表示返回列表的最大个数。那么,我们如果把1作为参数给进去,那么他返回的task就是当前 运行的那个task,然后从task中获取到最顶层的activity,这个activity就是当前显示给用户的那个activity了。
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); ComponentName cn = am.getRunningTasks(1).get(0).topActivity; Log.d("", "pkg:"+cn.getPackageName());Log.d("", "cls:"+cn.getClassName());
ActivityManager am= (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> runningTasks = am.getRunningTasks(1); ComponentName comName = runningTasks.get(0).topActivity;
String comNameCls = comName.getClassName();