摘要:安卓開發一般都需要進行的更新管理,常用操作老司機已為你封裝完畢,經常有小伙伴問怎么進行管理,那我告訴你,你可以用這份工具進行管理,具體可以查看源碼,現在為你開車,傳送門。
??安卓開發一般都需要進行apk的更新管理,常用操作老司機已為你封裝完畢,經常有小伙伴問怎么進行管理,那我告訴你,你可以用這份工具進行管理,具體可以查看源碼,現在為你開車,Demo傳送門。
站點 獲取App應用版本信息 → AppApplicationMgrgetAppName : 獲取本地apk的名稱 getVersionName : 獲取本地Apk版本名稱 getVersionCode : 獲取本地Apk版本號 getMetaData : 根據key獲取xml中Meta的值 getAppIcon : 獲取應用圖標 getAppFirstInstallTime : 獲取應用第一次安裝日期 getAppLastUpdateTime : 獲取應用更新日期 getAppSize : 獲取應用大小 getAppApk : 獲取應用apk文件 getAppInstaller : 獲取應用的安裝市場 getAppSign : 獲取應用簽名 getAppTargetSdkVersion : 獲取應用兼容sdk getAppUid : 獲取應用uid getNumCores : 獲取Cpu內核數 getRootPermission : 獲得root權限 getAppPermissions : 獲取應用的所有權限 hasPermission : 是否有權限 isInstalled : 應用是否安裝 installApk : 安裝應用 uninstallApk : 卸載應用 isSystemApp : 是否是系統應用 isServiceRunning : 服務是否在運行 stopRunningService : 停止服務 killProcesses : 結束進程 runScript : 運行腳本 runApp : 啟動應用 getPackageName : 獲得包名 getApplicationMetaData : 獲取application層級的metadata具體路線
@SuppressWarnings("rawtypes")
public class AppApplicationMgr {
/** * 獲取本地apk的名稱 * @param context 上下文 * @return String */ public static String getAppName(Context context) { try { PackageManager e = context.getPackageManager(); PackageInfo packageInfo = e.getPackageInfo(context.getPackageName(), 0); int labelRes = packageInfo.applicationInfo.labelRes; return context.getResources().getString(labelRes); } catch (NameNotFoundException var4) { var4.printStackTrace(); return "unKnown"; } } /** * 獲取本地Apk版本名稱 * @param context 上下文 * @return String */ public static String getVersionName(Context context) { String verName = ""; try { verName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName; } catch (NameNotFoundException e) { AppLogMessageMgr.e("AppApplicationMgr-->>getVerName()", e.getMessage() + "獲取本地Apk版本名稱失敗!"); e.printStackTrace(); } return verName; } /** * 獲取本地Apk版本號 * @param context 上下文 * @return int */ public static int getVersionCode(Context context) { int verCode = -1; try { verCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode; } catch (NameNotFoundException e) { AppLogMessageMgr.e("AppApplicationMgr-->>getVerCode()", e.getMessage() + "獲取本地Apk版本號失敗!"); e.printStackTrace(); } return verCode; } /** * 根據key獲取xml中Meta的值 * @param context 上下文 * @param key * @return */ public static String getMetaData(Context context, String key) { String value = ""; try { ApplicationInfo e = context.getPackageManager().getApplicationInfo(context.getPackageName(), 128); if(null != e) { Bundle metaData = e.metaData; if(null != metaData) { value = metaData.getString(key); if(null == value || value.length() == 0) { value = ""; } } } } catch (NameNotFoundException var5) { var5.printStackTrace(); } return value; } /** * 獲取應用圖標 * @param context * @param packageName * @return */ public static Drawable getAppIcon(Context context, String packageName) { PackageManager pm = context.getPackageManager(); Drawable appIcon = null; try { ApplicationInfo applicationInfo = pm.getApplicationInfo(packageName, 0); appIcon = applicationInfo.loadIcon(pm); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return appIcon; } /** * 獲取應用第一次安裝日期 * @param context * @param packageName * @return */ public static long getAppFirstInstallTime(Context context, String packageName) { long lastUpdateTime = 0; try { PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0); lastUpdateTime = packageInfo.firstInstallTime; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return lastUpdateTime; } /** * 獲取應用更新日期 * @param context * @param packageName * @return */ public static long getAppLastUpdateTime(Context context, String packageName) { long lastUpdateTime = 0; try { PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0); lastUpdateTime = packageInfo.lastUpdateTime; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return lastUpdateTime; } /** * 獲取應用大小 * @param context * @param packageName * @return */ public static long getAppSize(Context context, String packageName) { long appSize = 0; try { ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(packageName, 0); appSize = new File(applicationInfo.sourceDir).length(); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return appSize; } /** * 獲取應用apk文件 * @param context * @param packageName * @return */ public static String getAppApk(Context context, String packageName) { String sourceDir = null; try { ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(packageName, 0); sourceDir = applicationInfo.sourceDir; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return sourceDir; } /** * 獲取應用的安裝市場 * @param context * @param packageName * @return */ public static String getAppInstaller(Context context, String packageName) { return context.getPackageManager().getInstallerPackageName(packageName); } /** * 獲取應用簽名 * @param context * @param packageName * @return */ public static String getAppSign(Context context, String packageName) { try { PackageInfo pis = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES); return hexdigest(pis.signatures[0].toByteArray()); } catch (PackageManager.NameNotFoundException e) { throw new RuntimeException(AppApplicationMgr.class.getName() + "the " + packageName + ""s application not found"); } } public 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 ""; } /** * 獲取應用兼容sdk * @param context * @param packageName * @return */ public static int getAppTargetSdkVersion(Context context, String packageName) { try { PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0); ApplicationInfo applicationInfo = packageInfo.applicationInfo; return applicationInfo.targetSdkVersion; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return 0; } } /** * 獲取應用uid * @param context * @param packageName * @return */ public static int getAppUid(Context context, String packageName) { try { PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0); ApplicationInfo applicationInfo = packageInfo.applicationInfo; return applicationInfo.uid; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return 0; } } /** * 獲取Cpu內核數 * @return */ 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) { return Pattern.matches("cpu[0-9]", pathname.getName()); } }); return files.length; } catch (Exception e) { e.printStackTrace(); return 1; } } /** * 獲得root權限 * @param context * @return */ public static boolean getRootPermission(Context context) { String packageCodePath = context.getPackageCodePath(); Process process = null; DataOutputStream os = null; try { String cmd = "chmod 777 " + packageCodePath; process = Runtime.getRuntime().exec("su"); os = new DataOutputStream(process.getOutputStream()); os.writeBytes(cmd + " "); os.writeBytes("exit "); os.flush(); process.waitFor(); } catch (Exception e) { return false; } finally { try { if (os != null) { os.close(); } process.destroy(); } catch (Exception e) { e.printStackTrace(); } } return true; } /** * 獲取應用的所有權限 * @param context * @param packname * @return */ public static String[] getAppPermissions(Context context, String packname) { String[] requestedPermissions = null; try { PackageInfo info = context.getPackageManager().getPackageInfo(packname, PackageManager.GET_PERMISSIONS); requestedPermissions = info.requestedPermissions; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return requestedPermissions; } /** * 是否有權限 * @param context * @param permission * @return */ public static boolean hasPermission(Context context, String permission) { if (context != null && !TextUtils.isEmpty(permission)) { try { PackageManager packageManager = context.getPackageManager(); if (packageManager != null) { if (PackageManager.PERMISSION_GRANTED == packageManager.checkPermission(permission, context .getPackageName())) { return true; } Log.d("AppUtils", "Have you declared permission " + permission + " in AndroidManifest.xml ?"); } } catch (Exception e) { e.printStackTrace(); return false; } } return false; } /** * 應用是否安裝 * @param context * @param packageName * @return */ public static boolean isInstalled(Context context, String packageName) { boolean installed = false; if (TextUtils.isEmpty(packageName)) { return false; } ListinstalledApplications = context.getPackageManager().getInstalledApplications(0); for (ApplicationInfo in : installedApplications) { if (packageName.equals(in.packageName)) { installed = true; break; } else { installed = false; } } return installed; } /** * 安裝應用 * @param context * @param filePath * @return */ public static boolean installApk(Context context, String filePath) { File file = new File(filePath); if (!file.exists() || !file.isFile() || file.length() <= 0) { return false; } Intent i = new Intent(Intent.ACTION_VIEW); i.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive"); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); return true; } /** * 卸載應用 * @param context * @param packageName * @return */ public static boolean uninstallApk(Context context, String packageName) { if (TextUtils.isEmpty(packageName)) { return false; } Intent i = new Intent(Intent.ACTION_DELETE, Uri.parse("package:" + packageName)); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); return true; } /** * 是否是系統應用 * @param context * @param packageName * @return */ public static boolean isSystemApp(Context context, String packageName) { boolean isSys = false; PackageManager pm = context.getPackageManager(); try { ApplicationInfo applicationInfo = pm.getApplicationInfo(packageName, 0); if (applicationInfo != null && (applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) { isSys = true; } } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); isSys = false; } return isSys; } /** * 服務是否在運行 * @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 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; } /** * 結束進程 * @param context * @param pid * @param processName */ public static void killProcesses(Context context, int pid, String processName) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); String packageName; try { if (!processName.contains(":")) { packageName = processName; } else { packageName = processName.split(":")[0]; } activityManager.killBackgroundProcesses(packageName); Method forceStopPackage = activityManager.getClass().getDeclaredMethod("forceStopPackage", String.class); forceStopPackage.setAccessible(true); forceStopPackage.invoke(activityManager, packageName); } catch (Exception e) { e.printStackTrace(); } } /** * 運行腳本 * @param script * @return */ public static String runScript(String script) { String sRet; try { final Process m_process = Runtime.getRuntime().exec(script); final StringBuilder sbread = new StringBuilder(); Thread tout = new Thread(new Runnable() { public void run() { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(m_process.getInputStream()), 8192); String ls_1; try { while ((ls_1 = bufferedReader.readLine()) != null) { sbread.append(ls_1).append(" "); } } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }); tout.start(); final StringBuilder sberr = new StringBuilder(); Thread terr = new Thread(new Runnable() { public void run() { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(m_process.getErrorStream()), 8192); String ls_1; try { while ((ls_1 = bufferedReader.readLine()) != null) { sberr.append(ls_1).append(" "); } } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }); terr.start(); m_process.waitFor(); while (tout.isAlive()) { Thread.sleep(50); } if (terr.isAlive()) terr.interrupt(); String stdout = sbread.toString(); String stderr = sberr.toString(); sRet = stdout + stderr; } catch (Exception e) { e.printStackTrace(); return null; } return sRet; } /** * 啟動應用 * @param context * @param packagename */ public static void runApp(Context context, String packagename) { context.startActivity(new Intent(context.getPackageManager().getLaunchIntentForPackage(packagename))); }
// /**
// * 獲得當前版本信息
// * @param keyValues key信息
// * @return RequestParams
// */
// public static RequestParams getRequestParams(HashMap
// RequestParams params = new RequestParams();
// Iterator iterator = keyValues.entrySet().iterator();
// while(iterator.hasNext()){
// Map.Entry entry = (Map.Entry) iterator.next();
// Object key = entry.getKey();
// params.put((String) key, entry.getValue().toString());
// }
// return params;
// }
/** * 獲得包名 * * @param context 上下文 * @return 包名 */ public static String getPackageName(Context context) { try { return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).packageName; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return null; } /** * 獲取application層級的metadata * * @param context 上下文 * @param key key * @return value */ public static String getApplicationMetaData(Context context, String key) { try { Bundle metaData = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA).metaData; return metaData.get(key).toString(); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return null; } /** * 獲得應用申明的所有權限列表 * @param context 上下文 * @return 獲得應用申明的所有權限列表 */ public static ListgetPermissions(Context context){ List permissions=new ArrayList (); try { PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS); permissions.addAll(Arrays.asList(packageInfo.requestedPermissions)); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return permissions; }
}
終點站??好了,終點站到了,如果對本次旅途滿意的話,請給五星好評哦,沒關注的小伙伴輕輕點個上方的關注,畢竟老司機犧牲了很多時間才換來這么一份工具類,如果該工具類依賴其他工具類,都可以在我的史上最全的常用開發工具類收集(持續更新中)中找到。
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/67566.html
摘要:使用獲取應用和用戶系統信息此系列文章的應用示例已發布于可以或下載后運行查看歡迎使用幾個和模塊您可以收集有關用戶系統應用程序或屏幕的信息相關文檔的鏈接位于下面的示例中獲取應用信息支持進程主進程的模塊可用于獲取應用程序在用戶計算機上的位置 使用 Electron 獲取應用和用戶系統信息 此系列文章的應用示例已發布于 GitHub: electron-api-demos-Zh_CN. 可以 ...
摘要:寫在最后我前端剛入門沒多久,最近在公司實習,受到身邊同事影響,所以也開始寫文章來記錄自己的學習心得,這是我第一次寫文章,所以可能寫的不太好,大家對文章和代碼有什么建議歡迎提出來一起交流,謝謝 微信網頁授權并獲取用戶信息 介紹 在很多微信H5應用里,當用戶訪問第三方應用時就需要進行微信網頁授權,并且很多涉及安全的操作我們必須要先獲取用戶信息才能繼續,本文章簡單介紹了微信授權流程,并通過申...
閱讀 1924·2021-11-19 09:40
閱讀 2132·2021-10-09 09:43
閱讀 3294·2021-09-06 15:00
閱讀 2810·2019-08-29 13:04
閱讀 2766·2019-08-26 11:53
閱讀 3512·2019-08-26 11:46
閱讀 2320·2019-08-26 11:38
閱讀 390·2019-08-26 11:27