Created
October 4, 2017 20:49
-
-
Save AmandaRiu/94ce171cadc05797f8d819ca3b3fd0fb to your computer and use it in GitHub Desktop.
Android Utility Code Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Toggles the status of a component to enabled or disabled. Specifically used for enabling | |
* and disabling broadcast receivers. So in the case of a broadcast receiver listening for | |
* connectivity change...you may not want this always running, rather to toggle it on or off | |
* as needed. | |
* | |
* @param context Application-specific context | |
* @param componentClass The class to enable or disable | |
* @param enable Action to be taken on the class | |
*/ | |
public static void toggleComponent(Context context, Class componentClass, boolean enable) { | |
ComponentName componentName = new ComponentName(context, componentClass); | |
PackageManager pm = context.getPackageManager(); | |
pm.setComponentEnabledSetting(componentName, | |
enable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : | |
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, | |
PackageManager.DONT_KILL_APP); | |
} | |
/** | |
* Checks to see if a specific service is currently running | |
* | |
* @param context Application-specific context | |
* @param serviceClass The {@link android.app.Service} class to verify | |
*/ | |
public static boolean isServiceRunning(Context context, Class serviceClass) { | |
ActivityManager manager = | |
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); | |
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { | |
if (serviceClass.getName().equals(service.service.getClassName())) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* | |
* @return The build-generated name of the app. For example "V8Player-debug" | |
*/ | |
public static String getAppName(Context context) { | |
PackageManager pm = context.getPackageManager(); | |
ApplicationInfo info = null; | |
try { | |
info = pm.getApplicationInfo(context.getApplicationInfo().packageName, 0); | |
} catch (PackageManager.NameNotFoundException e) { | |
e.printStackTrace(); | |
} | |
return (String) (info != null ? pm.getApplicationLabel(info) : "Unknown"); | |
} | |
/** | |
* | |
* @return The total number of cores for this device | |
*/ | |
public static int getNumberCpuCores() { | |
return Runtime.getRuntime().availableProcessors(); | |
} | |
/** | |
* Converts a base size to density-specific. This allows for setting densityPixels | |
* programmatically. | |
* @param context The context | |
* @param dps The base size | |
* @return The dp size | |
*/ | |
public static int getDensityPixel(Context context, int dps) { | |
final float scale = context.getResources().getDisplayMetrics().density; | |
int pixels = (int) (dps * scale + 0.5f); | |
return pixels; | |
} | |
/** | |
* Checks if the device has an external application able to handle the intent provided. | |
* @param context The context | |
* @param intent The intent to validate | |
* @return True if the intent can be handled by an external app, else False. | |
*/ | |
public static boolean canDeviceHandleIntent(@NonNull Context context, | |
@NonNull Intent intent) { | |
final PackageManager mgr = context.getPackageManager(); | |
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { | |
return intent.resolveActivity(mgr) != null; | |
} else { | |
List<ResolveInfo> list = mgr.queryIntentActivities( | |
intent, PackageManager.MATCH_ALL); | |
return list.size() > 0; | |
} | |
} | |
/** | |
* Checks to see if the app as specified by the provided packageName is | |
* actively running by looking at the tasks in the ActivityManager. | |
*/ | |
public static boolean isAppRunning(String appPackageName) { | |
ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); | |
if (activityManager == null) { | |
return false; // Can't get activity manager | |
} | |
List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses(); | |
for (int idx = 0; idx < procInfos.size(); idx++) { | |
if (procInfos.get(idx).processName.equals(appPackageName)) { | |
return true; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment