Java tutorial
/* * Copyright (c) 2015. lizhijun (leo0618@126.com) * * 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 cn.whereyougo.framework.utils; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; import org.json.JSONObject; import android.app.ActivityManager; 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.SharedPreferences; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Build; import android.os.Parcelable; import android.text.TextUtils; /** * ??? ?AndroidManifest.xml ??<br/> * <br/> * 2015524 - ?3:50:38 * * @author lizhijun */ public class PackageManagerUtil { private static final String TAG = PackageManagerUtil.class.getSimpleName(); /** * ?AndroidManifestmeta-data * * @return ??() */ public static String getStringMetaData(Context ctx, String key) { if (ctx == null || TextUtils.isEmpty(key)) { return null; } String resultData = null; try { PackageManager packageManager = ctx.getPackageManager(); if (packageManager != null) { ApplicationInfo applicationInfo = packageManager.getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA); if (applicationInfo != null) { if (applicationInfo.metaData != null) { resultData = applicationInfo.metaData.getString(key); } } } } catch (PackageManager.NameNotFoundException e) { LogUtil.e(TAG, "", e); } return resultData; } /** * ?AndroidManifestmeta-data * * @return ??() */ public static int getIntMeta(final Context context, final String metaName, final int defaultValue) { int meta = defaultValue; try { ApplicationInfo appinfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA); if (appinfo != null) { meta = appinfo.metaData.getInt(metaName, defaultValue); } } catch (Exception e) { LogUtil.e(TAG, "", e); } return meta; } /** * ?AndroidManifest? * * @return ??()-1 */ public static int getPackageVersionCode(Context context) { int verCode = -1; PackageManager pm = context.getPackageManager(); PackageInfo pi; try { pi = pm.getPackageInfo(context.getPackageName(), 0); if (null != pi.versionName) { verCode = pi.versionCode; } } catch (NameNotFoundException e) { LogUtil.e(TAG, "", e); } return verCode; } /** * ?AndroidManifest??? * * @return ??()"" */ public static String getPackageVersionName(Context context) { String verName = ""; PackageManager pm = context.getPackageManager(); PackageInfo pi; try { pi = pm.getPackageInfo(context.getPackageName(), 0); if (null != pi.versionName) { verName = pi.versionName; } } catch (NameNotFoundException e) { LogUtil.e(TAG, "", e); } return verName; } /** * ??,json * * @param ctx */ public static JSONObject collectDeviceInfo(Context ctx) { JSONObject result = new JSONObject(); Field[] fields = Build.class.getDeclaredFields(); for (Field field : fields) { try { field.setAccessible(true); result.put(field.getName(), field.get(null).toString()); } catch (Exception e) { LogUtil.d(TAG, "an error occured when collect device info " + e.getMessage()); } } return result; } /** * ???? * * @param context */ public static boolean isAppOnForeground(final Context context) { ActivityManager activityManager = (ActivityManager) context.getApplicationContext() .getSystemService(Context.ACTIVITY_SERVICE); String packageName = context.getApplicationContext().getPackageName(); List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses != null) { for (RunningAppProcessInfo appProcess : appProcesses) { // The name of the process that this object is associated with. if (appProcess.processName.equals(packageName) && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { return true; } } } return false; } /** * * * @param context */ public static void exitApp(final Context context) { ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if (context.getPackageName().equals(service.service.getPackageName())) { Intent stopIntent = new Intent(); ComponentName serviceCMP = service.service; stopIntent.setComponent(serviceCMP); context.stopService(stopIntent); break; } } android.os.Process.killProcess(android.os.Process.myPid()); System.exit(0); } /** * apk? * * @return truefalse */ public static boolean checkApkIsInstalled(Context context, String packageName) { boolean result = false; try { PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); if (packageInfo != null) { result = true; } } catch (NameNotFoundException e) { LogUtil.e(TAG, TAG + "--->Exception:" + e.getMessage()); } return result; } /** * ?apk? * * @return */ public static int getVersionCode(Context context, String packageName) { int versionCode = -1; if (packageName == null || "".equals(packageName)) { return versionCode; } try { PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); versionCode = info.versionCode; } catch (NameNotFoundException e) { LogUtil.e(TAG, TAG + "--->Exception:" + e.getMessage()); } return versionCode; } /** * ?? ??????? * * @return */ public static List<PackageInfo> getInstalledUserApps(Context context) { PackageManager packageManager = context.getPackageManager(); List<PackageInfo> result = new ArrayList<PackageInfo>(); List<PackageInfo> list = packageManager.getInstalledPackages(0);// ? for (PackageInfo packageInfo : list) { if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) { // ??? result.add(packageInfo); } } return result; } /** * ???? * * @param mContext * * @param TagClass * ??? * @param iconResId * ?? * @param iconName * ?? */ public static void addShortCut2Desktop(Context mContext, Class<?> TagClass, int iconResId, String iconName) { SharedPreferences sp = mContext.getSharedPreferences("appinfo", Context.MODE_PRIVATE); if (!sp.getBoolean("shortcut_flag_icon", false)) { sp.edit().putBoolean("shortcut_flag_icon", true).commit(); LogUtil.d("shortcut", "first create successfull"); } else { LogUtil.d("shortcut", "no created"); return; } String ACTION_ADD_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT"; Intent intent = new Intent(); intent.setClass(mContext, TagClass); intent.setAction("android.intent.action.MAIN"); intent.addCategory("android.intent.category.LAUNCHER"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); Intent addShortcut = new Intent(ACTION_ADD_SHORTCUT); Parcelable icon = Intent.ShortcutIconResource.fromContext(mContext, iconResId);// ??? addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, iconName);// ?? addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);// ?? addShortcut.putExtra("duplicate", false);// ???? addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);// ?? mContext.sendBroadcast(addShortcut);// ?? } /** * ???? * * @return */ public static boolean checkMainProcess(Context context) { final String curProcessName = getProcessNameByPid(context, android.os.Process.myPid()); return isMainProcess(context, curProcessName); } /** * ? * * @param context * @param processName * ????+?? * @return */ public static boolean isMainProcess(Context context, String processName) { final String mainProcess = context.getApplicationInfo().processName; return mainProcess.equals(processName); } /** * ????<br/> * ?id??????+?? * * @param context * @return */ public static String getCurProcessName(Context context) { final int curPid = android.os.Process.myPid(); String curProcessName = getProcessNameByPid(context, curPid); if (null == curProcessName) { curProcessName = context.getApplicationInfo().processName; LogUtil.d(TAG, "getCurProcessName,no find process,curPid=", curPid, ",curProcessName=", curProcessName); } return curProcessName; } /** * ???<br/> * id??????+?? * * @param context * @param pid * @return */ public static String getProcessNameByPid(Context context, final int pid) { String processName = null; ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses != null) { for (RunningAppProcessInfo appProcess : appProcesses) { if (pid == appProcess.pid) { processName = appProcess.processName; break; } } } LogUtil.d(TAG, "getProcessNameByPid,pid=", pid, ",processName=", processName); return processName; } /** * ??????? * * @param context * @return */ public static boolean checkTheProcess(final Context context, String endProcessName) { ActivityManager activityManager = (ActivityManager) context.getApplicationContext() .getSystemService(Context.ACTIVITY_SERVICE); int myPid = android.os.Process.myPid(); List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses != null) { for (RunningAppProcessInfo appProcess : appProcesses) { if (myPid == appProcess.pid) { LogUtil.d(TAG, "process.pid appProcess.processName=" + appProcess.processName + ", endProcessName=" + endProcessName); if (appProcess.processName.endsWith(endProcessName)) { return true; } break; } } } return false; } /** * ??activity * * @param context * @return */ @SuppressWarnings("deprecation") public static ComponentName getTheProcessBaseActivity(final Context context) { ActivityManager activityManager = (ActivityManager) context.getApplicationContext() .getSystemService(Context.ACTIVITY_SERVICE); RunningTaskInfo task = activityManager.getRunningTasks(1).get(0); if (task.numActivities > 0) { LogUtil.d(TAG, "runningActivity topActivity=" + task.topActivity.getClassName()); LogUtil.d(TAG, "runningActivity baseActivity=" + task.baseActivity.getClassName()); return task.baseActivity; } return null; } }