在Android N之后已经不再适用,新的方法抽个时间研究下
/**
* 判断当前界面是否是桌面
*/
public static booleanisHome(Context ctx) {
ActivityManager mActivityManager = (ActivityManager)ctx.getSystemService(Context.ACTIVITY_SERVICE);
List rti = mActivityManager.getRunningTasks(1);
returngetHomes(ctx).contains(rti.get(0).topActivity.getPackageName());
}
/**
* 获得属于桌面的应用的应用包名称
*
* @return返回包含所有包名的字符串列表
*/
private staticListgetHomes(Context ctx) {
List names =newArrayList();
PackageManager packageManager = ctx.getPackageManager();
Intent intent =newIntent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
List resolveInfo = packageManager.queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY);
for(ResolveInfo ri : resolveInfo) {
names.add(ri.activityInfo.packageName);
}
returnnames;
}