Skip to content

Instantly share code, notes, and snippets.

@Krishan14sharma
Created August 13, 2015 12:13
Show Gist options
  • Save Krishan14sharma/e580102e124a5c3767c7 to your computer and use it in GitHub Desktop.
Save Krishan14sharma/e580102e124a5c3767c7 to your computer and use it in GitHub Desktop.
public class DetectHomeScreen {
private static List<String> homePackageNamesCache;
public static List<String> homePackageName(Context context){
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
List<ResolveInfo> info = context.getPackageManager().queryIntentActivities(intent, 0);
List<String> homes = new ArrayList<String>(info.size());
for(int i=0; i<info.size(); i++){
homes.add(info.get(i).activityInfo.packageName);
}
homes.add(DetectHomeScreen.class.getPackage().getName());
return homes;
}
public static boolean isHomeRunning(Context context){
if (homePackageNamesCache==null)
homePackageNamesCache = homePackageName(context);
return isHomeRunning(context, homePackageNamesCache);
}
public static boolean isHomeRunning(Context context, List<String> homePackages){
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
String topPackage = am.getRunningTasks(1).get(0).topActivity.getPackageName();
return homePackages.contains(topPackage);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment