boolean isHome = isHome(context, getHomes(context));



private static List<String> getHomes(Context context) {
			List<String> names = new ArrayList<String>();
			PackageManager packageManager = context.getPackageManager();
			// 属性
			Intent intent = new Intent(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_HOME);
			List<ResolveInfo> resolveInfo = packageManager
					.queryIntentActivities(intent,
							PackageManager.MATCH_DEFAULT_ONLY);
			for (ResolveInfo ri : resolveInfo) {
				names.add(ri.activityInfo.packageName);
			}
			return names;
		}

		private static boolean isHome(Context context,
				List<String> homePackageNames) {
			ActivityManager mActivityManager = (ActivityManager) context
					.getSystemService(Context.ACTIVITY_SERVICE);
			List<RunningTaskInfo> rti = mActivityManager.getRunningTasks(1);
			if (rti.size() > 0) {
				return homePackageNames.contains(rti.get(0).topActivity
						.getPackageName());
			} else {
				return false;
			}
		}




需要权限。 

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()