我想知道我的手机是否在带有isDesktop()的桌面页面上,而logcat给出了false,如下所示。我想知道我的代码有什么问题。它在我的模拟器API 21中运行良好,但在我的手机Android 4.4.2中保持错误。
public boolean isDesktop() {
Log.e("KOST", "Check");
String[] activePackages;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
activePackages = getActivePackages();
} else {
activePackages = getActivePackagesCompat();
}
Log.e("KOST", Arrays.toString(activePackages));
if (activePackages != null) {
for (String activePackage : activePackages) {
if (activePackage.equals("com.android.launcher")) {
Log.e("isDesktop", "true");
return true;
}
}
}
Log.e("isDesktop", "false");
return false;
}
String[] getActivePackagesCompat() {
final List<ActivityManager.RunningTaskInfo> taskInfo = am
.getRunningTasks(1);
final ComponentName componentName = taskInfo.get(0).topActivity;
final String[] activePackages = new String[1];
activePackages[0] = componentName.getPackageName();
return activePackages;
}
String[] getActivePackages() {
final Set<String> activePackages = new HashSet<String>();
final List<ActivityManager.RunningAppProcessInfo> processInfos = am
.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
activePackages.addAll(Arrays.asList(processInfo.pkgList));
}
}
return activePackages.toArray(new String[activePackages.size()]);
}
下面是logcat输出:
03-31 18:17:14.256 13148-13194/com.example.jackey.trafficmaster E/KOST﹕ Check
03-31 18:17:14.261 13148-13194/com.example.jackey.trafficmaster E/KOST﹕ [com.sec.android.app.launcher]
03-31 18:17:14.261 13148-13194/com.example.jackey.trafficmaster E/isDesktop﹕ false
03-31 18:17:14.261 13148-13194/com.example.jackey.trafficmaster E/KOST﹕ Check
03-31 18:17:14.271 13148-13194/com.example.jackey.trafficmaster E/KOST﹕ [com.sec.android.app.launcher]
03-31 18:17:14.271 13148-13194/com.example.jackey.trafficmaster E/isDesktop﹕ false
03-31 18:17:14.271 13148-13194/com.example.jackey.trafficmaster E/KOST﹕ Check
03-31 18:17:14.271 13148-13194/com.example.jackey.trafficmaster E/KOST﹕ [com.sec.android.app.launcher]
03-31 18:17:14.276 13148-13194/com.example.jackey.trafficmaster E/isDesktop﹕ false