android获取最近使用应用的信息

ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE) ;

List appList4 = mActivityManager
.getRecentTasks(100, 1);
for (ActivityManager.RecentTaskInfo running : appList4) {
System.out.println(running.origActivity.getClassName());
}
这个方法有个最坑爹的地方,我不理解他们是怎么搞的,都没试过的吧,running.origActivity这个是输出null来的,有大牛知道的指点下
后来我参照了别人的方法,这样才可以
PackageManager pm = this.getPackageManager();
ActivityManager mActivityManager = (ActivityManager) MainActivity.this
.getSystemService(Context.ACTIVITY_SERVICE);
List appList4 = mActivityManager
.getRecentTasks(5, ActivityManager.RECENT_WITH_EXCLUDED);//参数,前一个是你要取的最大数,后一个是状态
for (ActivityManager.RecentTaskInfo running : appList4) {
Intent intent = running.baseIntent;
ResolveInfo resolveInfo = pm.resolveActivity(intent, 0);
if (resolveInfo != null)

System.out.println(resolveInfo.activityInfo.packageName "n");//获取应用包名
System.out.println(resolveInfo.loadLabel(pm).toString() "n");//获取应用名
// System.out.println(resolveInfo.loadIcon(pm) "n");//获取应用头标

}

最后在Mainfest声明权限

原文地址: http://my.eoe.cn/693212/archive/4670.html

你可能感兴趣的:(android获取最近使用应用的信息)