List of usage examples for android.content ComponentName getPackageName
public @NonNull String getPackageName()
From source file:Main.java
public static boolean isApplicationInBackground(Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> taskList = am.getRunningTasks(1); if (taskList != null && !taskList.isEmpty()) { ComponentName topActivity = taskList.get(0).topActivity; if (topActivity != null && !topActivity.getPackageName().equals(context.getPackageName())) { return true; }/*w w w . j a v a2 s . c o m*/ } return false; }
From source file:Main.java
/** * @return true if the device or profile is already owned *//* w w w. j av a2 s. c o m*/ public static boolean isManaged(Context context) { DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context .getSystemService(Context.DEVICE_POLICY_SERVICE); List<ComponentName> admins = devicePolicyManager.getActiveAdmins(); if (admins == null) return false; for (ComponentName admin : admins) { String adminPackageName = admin.getPackageName(); if (devicePolicyManager.isDeviceOwnerApp(adminPackageName) || devicePolicyManager.isProfileOwnerApp(adminPackageName)) { return true; } } return false; }
From source file:Main.java
public static boolean isMyLauncherDefault(Context context) { final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN); filter.addCategory(Intent.CATEGORY_HOME); List<IntentFilter> filters = new ArrayList<IntentFilter>(); filters.add(filter);/*from ww w .ja va 2 s. c om*/ final String myPackageName = context.getPackageName(); List<ComponentName> activities = new ArrayList<>(); PackageManager packageManager = (PackageManager) context.getPackageManager(); // You can use name of your package here as third argument packageManager.getPreferredActivities(filters, activities, null); if (activities.size() == 0) //no default return true; for (ComponentName activity : activities) { if (myPackageName.equals(activity.getPackageName())) { return true; } } return false; }
From source file:Main.java
/** * Checks if the application is in the background (i.e behind another application's Activity). * * @param context {@link Context}/*from w w w .java 2s . c om*/ * @return true if another application is above this one. */ public static boolean isApplicationBroughtToBackground(final Context context) { if (context.checkCallingOrSelfPermission(Manifest.permission.GET_TASKS) == PackageManager.PERMISSION_DENIED) return false; ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getPackageName()) && !topActivity.getPackageName().equals("com.google.android.voicesearch")) { return true; } } return false; }
From source file:Main.java
/** * Method checks if the app is in background or not *///from w ww. ja v a 2 s .co m public static boolean isAppIsInBackground(Context context) { boolean isInBackground = true; ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) { List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses(); for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) { if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { for (String activeProcess : processInfo.pkgList) { if (activeProcess.equals(context.getPackageName())) { isInBackground = false; } } } } } else { List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1); ComponentName componentInfo = taskInfo.get(0).topActivity; if (componentInfo.getPackageName().equals(context.getPackageName())) { isInBackground = false; } } return isInBackground; }
From source file:Main.java
public static String getCurrentActivityPkgName(Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); ComponentName cn = am.getRunningTasks(1).get(0).topActivity; // String className=cn.getClassName(); return cn.getPackageName(); }
From source file:Main.java
/** * Judge whether an app is in background * * @param context//from w w w . j ava 2 s . c o m * @return */ public static boolean isAppInBackground(Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> taskList = am.getRunningTasks(1); if (taskList != null && !taskList.isEmpty()) { ComponentName topActivity = taskList.get(0).topActivity; if (topActivity != null && !topActivity.getPackageName().equals(context.getPackageName())) { return true; } } return false; }
From source file:Main.java
public static String getAppVersion(Context context) { String version = ""; // get current version try {/*from w ww. ja va2s .c o m*/ ComponentName comp = new ComponentName(context, ""); PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(), 0); version = pinfo.versionName; } catch (NameNotFoundException e) { e.printStackTrace(); } return version; }
From source file:Main.java
public static String getVersion(Context context) { String version = ""; // get current version try {//from w ww . j a v a2s.c om ComponentName comp = new ComponentName(context, ""); PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(), 0); version = pinfo.versionName; } catch (NameNotFoundException e) { e.printStackTrace(); } return version; }
From source file:Main.java
/** * whether application is in background//from w w w. j a va 2 s. com * <ul> * <li>need use permission android.permission.GET_TASKS in Manifest.xml</li> * </ul> * * @param context * @return if application is in background return true, otherwise return * false */ public static boolean isApplicationInBackground(Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> taskList = am.getRunningTasks(1); if (taskList != null && !taskList.isEmpty()) { ComponentName topActivity = taskList.get(0).topActivity; if (topActivity != null && !topActivity.getPackageName().equals(context.getPackageName())) { return true; } } return false; }