Back to project page android-utils.
The source code is released under:
Apache License
If you think the Android project android-utils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** * Copyright 2014 Zhenguo Jin/* w w w . ja va 2 s .c o m*/ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.worthed.util; import java.io.File; import java.io.FileFilter; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.security.MessageDigest; import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; import android.app.ActivityManager; import android.app.ActivityManager.MemoryInfo; import android.app.ActivityManager.RunningAppProcessInfo; import android.app.ActivityManager.RunningServiceInfo; import android.app.ActivityManager.RunningTaskInfo; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.net.Uri; import android.text.TextUtils; /** * APP??? * APP???????????????????? * * @author jingle1267@163.com */ public class AppUtils { private static final boolean DEBUG = true; private static final String TAG = "AppUtils"; /** * ????????? * * @param context ??? * @return ?????Code */ public static int getVerCode(Context context) { int verCode = -1; try { String packageName = context.getPackageName(); verCode = context.getPackageManager().getPackageInfo(packageName, 0).versionCode; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return verCode; } /** * ???????????? * * @param context ??? * @return ????????? */ public static String getVerName(Context context) { String verName = ""; try { String packageName = context.getPackageName(); verName = context.getPackageManager().getPackageInfo(packageName, 0).versionName; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return verName; } /** * ??apk * * @param context ??? * @param file APK?? */ public static void installApk(Context context, File file) { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); context.startActivity(intent); } /** * ??apk * * @param context ??? * @param file APK??uri */ public static void installApk(Context context, Uri file) { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(file, "application/vnd.android.package-archive"); context.startActivity(intent); } /** * ????apk * * @param context ??? * @param packageName ???? */ public static void uninstallApk(Context context, String packageName) { Intent intent = new Intent(Intent.ACTION_DELETE); Uri packageURI = Uri.parse("package:" + packageName); intent.setData(packageURI); context.startActivity(intent); } /** * ???????????? * * @param context ??? * @param className ???? * @return ??????????? */ public static boolean isServiceRunning(Context context, String className) { boolean isRunning = false; ActivityManager activityManager = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); List<RunningServiceInfo> servicesList = activityManager .getRunningServices(Integer.MAX_VALUE); for (RunningServiceInfo si : servicesList) { if (className.equals(si.service.getClassName())) { isRunning = true; } } return isRunning; } /** * ?????????? * * @param context ??? * @param className ???? * @return ????????? */ public static boolean stopRunningService(Context context, String className) { Intent intent_service = null; boolean ret = false; try { intent_service = new Intent(context, Class.forName(className)); } catch (Exception e) { e.printStackTrace(); } if (intent_service != null) { ret = context.stopService(intent_service); } return ret; } /** * ??CPU??? * * @return CPU??? */ public static int getNumCores() { try { File dir = new File("/sys/devices/system/cpu/"); File[] files = dir.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { if (Pattern.matches("cpu[0-9]", pathname.getName())) { return true; } return false; } }); return files.length; } catch (Exception e) { return 1; } } /** * whether this process is named with processName * * @param context ??? * @param processName ????? * @return <ul> * return whether this process is named with processName * <li>if context is null, return false</li> * <li>if {@link ActivityManager#getRunningAppProcesses()} is null, * return false</li> * <li>if one process of * {@link ActivityManager#getRunningAppProcesses()} is equal to * processName, return true, otherwise return false</li> * </ul> */ public static boolean isNamedProcess(Context context, String processName) { if (context == null || TextUtils.isEmpty(processName)) { return false; } int pid = android.os.Process.myPid(); ActivityManager manager = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> processInfoList = manager .getRunningAppProcesses(); if (processInfoList == null) { return true; } for (RunningAppProcessInfo processInfo : manager .getRunningAppProcesses()) { if (processInfo.pid == pid && processName.equalsIgnoreCase(processInfo.processName)) { return true; } } return false; } /** * whether application is in background * <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; } /** * ?????????? * * @param context ??? * @param pkgName ???? */ public static String getSign(Context context, String pkgName) { try { PackageInfo pis = context.getPackageManager().getPackageInfo( pkgName, PackageManager.GET_SIGNATURES); return hexdigest(pis.signatures[0].toByteArray()); } catch (NameNotFoundException e) { e.printStackTrace(); return null; } } /** * ??????????????????32?????? * * @param paramArrayOfByte ????byte?? * @return 32????????? */ private static String hexdigest(byte[] paramArrayOfByte) { final char[] hexDigits = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102}; try { MessageDigest localMessageDigest = MessageDigest.getInstance("MD5"); localMessageDigest.update(paramArrayOfByte); byte[] arrayOfByte = localMessageDigest.digest(); char[] arrayOfChar = new char[32]; for (int i = 0, j = 0; ; i++, j++) { if (i >= 16) { return new String(arrayOfChar); } int k = arrayOfByte[i]; arrayOfChar[j] = hexDigits[(0xF & k >>> 4)]; arrayOfChar[++j] = hexDigits[(k & 0xF)]; } } catch (Exception e) { e.printStackTrace(); } return ""; } /** * ???????????????? * * @param context ???????context * @return ????????? */ public static int gc(Context context) { long i = getDeviceUsableMemory(context); int count = 0; // ????????? ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); // ??????????service?? List<RunningServiceInfo> serviceList = am.getRunningServices(100); if (serviceList != null) for (RunningServiceInfo service : serviceList) { if (service.pid == android.os.Process.myPid()) continue; try { android.os.Process.killProcess(service.pid); count++; } catch (Exception e) { e.getStackTrace(); } } // ?????????????? List<RunningAppProcessInfo> processList = am.getRunningAppProcesses(); if (processList != null) for (RunningAppProcessInfo process : processList) { // ??????RunningAppProcessInfo.IMPORTANCE_SERVICE??????????????? // ??????RunningAppProcessInfo.IMPORTANCE_VISIBLE???????????????????????????????? if (process.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) { // pkgList ?????????????? String[] pkgList = process.pkgList; for (String pkgName : pkgList) { if (DEBUG) { LogUtils.d(TAG, "======???????????" + pkgName); } try { am.killBackgroundProcesses(pkgName); count++; } catch (Exception e) { // ????????? e.getStackTrace(); } } } } if (DEBUG) { LogUtils.d(TAG, "?????" + (getDeviceUsableMemory(context) - i) + "M??"); } return count; } /** * ???????????????? * * @param context ???????context * @return ???????? */ public static int getDeviceUsableMemory(Context context) { ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); MemoryInfo mi = new MemoryInfo(); am.getMemoryInfo(mi); // ?????????????? return (int) (mi.availMem / (1024 * 1024)); } /** * ???????????? * * @param context ??? * @return ??????List */ public static List<PackageInfo> getAllApps(Context context) { List<PackageInfo> apps = new ArrayList<PackageInfo>(); PackageManager pManager = context.getPackageManager(); List<PackageInfo> paklist = pManager.getInstalledPackages(0); for (int i = 0; i < paklist.size(); i++) { PackageInfo pak = paklist.get(i); if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) { // customs applications apps.add(pak); } } return apps; } /** * ????????SDK?? * * @return ?API 17 ??? 17 */ public static int getSDKVersion() { return android.os.Build.VERSION.SDK_INT; } /** * ????Dalvik??? * * @return ?? */ public static boolean isDalvik() { return "Dalvik".equals(getCurrentRuntimeValue()); } /** * ????ART??? * * @return ?? */ public static boolean isART() { String currentRuntime = getCurrentRuntimeValue(); return "ART".equals(currentRuntime) || "ART debug build".equals(currentRuntime); } /** * ??????????Runtime * * @return ?????????????Dalvik, ART, ART debug build; */ public static String getCurrentRuntimeValue() { try { Class<?> systemProperties = Class.forName("android.os.SystemProperties"); try { Method get = systemProperties.getMethod("get", String.class, String.class); if (get == null) { return "WTF?!"; } try { final String value = (String) get.invoke( systemProperties, "persist.sys.dalvik.vm.lib", /* Assuming default is */"Dalvik"); if ("libdvm.so".equals(value)) { return "Dalvik"; } else if ("libart.so".equals(value)) { return "ART"; } else if ("libartd.so".equals(value)) { return "ART debug build"; } return value; } catch (IllegalAccessException e) { return "IllegalAccessException"; } catch (IllegalArgumentException e) { return "IllegalArgumentException"; } catch (InvocationTargetException e) { return "InvocationTargetException"; } } catch (NoSuchMethodException e) { return "SystemProperties.get(String key, String def) method is not found"; } } catch (ClassNotFoundException e) { return "SystemProperties class is not found"; } } }