/** * Return whether application is running. * * @param pkgName The name of the package. * @return {@code true}: yes<br>{@code false}: no */ public static boolean isAppRunning(@NonNull final String pkgName) { int uid; PackageManager packageManager = XApp.getApp().getPackageManager(); try { ApplicationInfo ai = packageManager.getApplicationInfo(pkgName, 0); if (ai == null) return false; uid = ai.uid; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return false; } ActivityManager am = (ActivityManager) XApp.getApp().getSystemService(Context.ACTIVITY_SERVICE); if (am != null) { List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(Integer.MAX_VALUE); if (taskInfo != null && taskInfo.size() > 0) { for (ActivityManager.RunningTaskInfo aInfo : taskInfo) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (pkgName.equals(aInfo.baseActivity.getPackageName())) { return true; } } } } List<ActivityManager.RunningServiceInfo> serviceInfo = am.getRunningServices(Integer.MAX_VALUE); if (serviceInfo != null && serviceInfo.size() > 0) { for (ActivityManager.RunningServiceInfo aInfo : serviceInfo) { if (uid == aInfo.uid) { return true; } } } } return false; }
获取APP图标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/** * Return the application's icon. * * @param packageName The name of the package. * @return the application's icon */ public static Drawable getAppIcon(final String packageName) { if (isSpace(packageName)) return null; try { PackageManager pm = XApp.getApp().getPackageManager(); PackageInfo pi = pm.getPackageInfo(packageName, 0); return pi == null ? null : pi.applicationInfo.loadIcon(pm); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return null; } }
获取APP名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/** * Return the application's name. * * @param packageName The name of the package. * @return the application's name */ public static String getAppName(final String packageName) { if (isSpace(packageName)) return ""; try { PackageManager pm = XApp.getApp().getPackageManager(); PackageInfo pi = pm.getPackageInfo(packageName, 0); return pi == null ? null : pi.applicationInfo.loadLabel(pm).toString(); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return ""; } }
获取APP路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/** * Return the application's path. * * @param packageName The name of the package. * @return the application's path */ public static String getAppPath(final String packageName) { if (isSpace(packageName)) return ""; try { PackageManager pm = XApp.getApp().getPackageManager(); PackageInfo pi = pm.getPackageInfo(packageName, 0); return pi == null ? null : pi.applicationInfo.sourceDir; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return ""; } }
获取APP版名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/** * Return the application's version name. * * @param packageName The name of the package. * @return the application's version name */ public static String getAppVersionName(final String packageName) { if (isSpace(packageName)) return ""; try { PackageManager pm = XApp.getApp().getPackageManager(); PackageInfo pi = pm.getPackageInfo(packageName, 0); return pi == null ? null : pi.versionName; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return ""; } }
获取APP版本号
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/** * Return the application's version code. * * @param packageName The name of the package. * @return the application's version code */ public static int getAppVersionCode(final String packageName) { if (isSpace(packageName)) return -1; try { PackageManager pm = XApp.getApp().getPackageManager(); PackageInfo pi = pm.getPackageInfo(packageName, 0); return pi == null ? -1 : pi.versionCode; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return -1; } }
获取APP签名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/** * Return the application's signature. * * @param packageName The name of the package. * @return the application's signature */ public static Signature[] getAppSignature(final String packageName) { if (isSpace(packageName)) return null; try { PackageManager pm = XApp.getApp().getPackageManager(); @SuppressLint("PackageManagerGetSignatures") PackageInfo pi = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES); return pi == null ? null : pi.signatures; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return null; } }
获取APP的UI-ID
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/** * Return the application's user-ID. * * @param pkgName The name of the package. * @return the application's signature for MD5 value */ public static int getAppUid(String pkgName) { try { ApplicationInfo ai = XApp.getApp().getPackageManager().getApplicationInfo(pkgName, 0); if (ai != null) { return ai.uid; } } catch (Exception e) { e.printStackTrace(); } return -1; }