概述
import android.annotation.SuppressLint; 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.content.pm.ResolveInfo; import android.content.pm.Signature; import android.graphics.drawable.Drawable; import android.net.Uri; import android.text.TextUtils; import com.olemob.activity.OlemobApplication; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class AppInfoUtil { /** * 是否为系统App * * @param info * @return */ public static boolean isSystemApp(PackageInfo info) { return null != info && ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0); } /** * 是否为系统软件的更新软件 * * @param info * @return */ public static boolean isSystemUpdateApp(PackageInfo info) { return null != info && ((info.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0); } /** * 是否为非系统应用 * * @param info * @return */ public static boolean isUserApp(PackageInfo info) { return (!isSystemApp(info) && !isSystemUpdateApp(info)); } /** * 是否为系统应用 * * @param info * @return */ public static boolean isSysApp(PackageInfo info) { return isSystemApp(info) || isSystemUpdateApp(info); } /** * 是否为非系统应用 * * @param pkgName * @return */ public static boolean isUserApp(String pkgName) { return !isSystemApp(pkgName); } /** * 判断某个应用是否是系统应用或者系统升级应用 * * @param pkgName * @return */ public static boolean isSystemApp(String pkgName) { try { PackageInfo pInfo = OlemobApplication.getInstance().getPackageManager().getPackageInfo(pkgName, 0); return ((pInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) || ((pInfo.applicationInfo .flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0); } catch (NameNotFoundException e) { LogUtil.e(e); } return false; } /** * 根据包名得到App名 * * @param context * @param pkgName * @return AppName */ public static String getAppNameByPkgName(Context context, String pkgName) { if (TextUtils.isEmpty(pkgName)) { return ""; } String name = null; PackageManager pm = context.getPackageManager(); try { ApplicationInfo info = pm.getApplicationInfo(pkgName, PackageManager.PERMISSION_GRANTED); name = (String)pm.getApplicationLabel(info); } catch (NameNotFoundException e) { LogUtil.e(e); } return name; } /** * 根据包名获得版本号 * * @param context * @param pkgName * @return * @since 2015年6月4日 */ public static int getVersionCode(Context context, String pkgName) { if (TextUtils.isEmpty(pkgName)) { return 0; } int verName; try { PackageManager pm = context.getPackageManager(); PackageInfo pi = pm.getPackageInfo(pkgName, 0); if (pi == null) { return 0; } else { verName = pi.versionCode; return verName; } } catch (Exception ignored) { } return 0; } /** * 根据包名获得版本号 * * @param context * @param pkgName * @return * @since 2015年6月4日 */ public static String getVersionName(Context context, String pkgName) { if (TextUtils.isEmpty(pkgName)) { return null; } String verName; try { PackageManager pm = context.getPackageManager(); PackageInfo pi = pm.getPackageInfo(pkgName, 0); if (pi == null) { return null; } else { verName = pi.versionName; return verName; } } catch (Exception ignored) { } return null; } public static Drawable getAppIcon(Context context, String pkgName) { if (TextUtils.isEmpty(pkgName)) { return null; } Drawable icon = null; PackageManager pm = context.getPackageManager(); try { PackageInfo info = pm.getPackageInfo(pkgName, PackageManager.PERMISSION_GRANTED); icon = info.applicationInfo.loadIcon(pm); } catch (Exception e) { LogUtil.e(e); } return icon; } /** * 批量获取应用图标。<br> * <font color=red>注意:不要一次获取过多</font> * * @param context * @param pkgs * @return * @since 2015年6月3日 */ public static List<Drawable> getAppIcon(Context context, List<String> pkgs) { if (pkgs == null || pkgs.isEmpty()) { return null; } List<Drawable> iconList = new ArrayList<>(); PackageManager pm = context.getPackageManager(); try { for (String pkgName : pkgs) { PackageInfo info = pm.getPackageInfo(pkgName, PackageManager.PERMISSION_GRANTED); Drawable icon = info.applicationInfo.loadIcon(pm); iconList.add(icon); } } catch (Exception e) { LogUtil.e(e); } return iconList; } /** * 当前 是否可以 isDisabled * If app's state is enable, you can disable it. or you can enable it. * if return true, you can enable the app; * if return false,you can disable the app.(不知道语法对不对,先这样写吧) * * @param context * @param pkgName * @return */ public static boolean isSystemAppDisabled(Context context, String pkgName) { boolean isDisabled = false; try { PackageManager pkgMgr = context.getPackageManager(); if (pkgMgr != null) { @SuppressLint("PackageManagerGetSignatures") PackageInfo pkgInfo = pkgMgr .getPackageInfo(pkgName, PackageManager.GET_SIGNATURES); @SuppressLint("PackageManagerGetSignatures") PackageInfo sysPkgInfo = pkgMgr .getPackageInfo("android", PackageManager.GET_SIGNATURES); if (pkgInfo != null && pkgInfo.signatures != null && pkgInfo.signatures.length > 0 && sysPkgInfo != null && sysPkgInfo.signatures != null && sysPkgInfo.signatures.length > 0) { isDisabled = (sysPkgInfo.signatures[0] .equals(pkgInfo.signatures[0])) || (!pkgInfo.applicationInfo.enabled);// 未开启 } } } catch (Exception e) { LogUtil.e(e); } return isDisabled; } /** * 判断一个Intent是否可用。 * * @param context 上下文 * @param intent Intent * @return * @since 2014年12月22日 */ public static boolean isIntentAvailable(final Context context, final Intent intent) { final PackageManager packageManager = context.getPackageManager(); List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent, PackageManager.GET_INTENT_FILTERS); return resolveInfo.size() > 0; } /** * 查看某一应用 Disable or enabled * * @param context * @param pkgName * @return */ public static boolean getAppleState(Context context, String pkgName) { boolean isEnable = true; try { PackageManager pkgMgr = context.getPackageManager(); if (pkgMgr != null) { PackageInfo pkgInfo = pkgMgr.getPackageInfo(pkgName, 0); if (pkgInfo != null) { ApplicationInfo appInfo = pkgInfo.applicationInfo; if (appInfo != null) { isEnable = appInfo.enabled; } } } } catch (Exception e) { LogUtil.e(e); } return isEnable; } /** * 获取安装的 应用总数 * * @param context * @return */ public static int getAllAppCount(Context context) { int appCount = 0; try { PackageManager pkgMgr = context.getPackageManager(); if (pkgMgr != null) { List<PackageInfo> apps = pkgMgr.getInstalledPackages(0); if (apps != null) { appCount = apps.size(); } } } catch (Exception e) { LogUtil.e(e); } return appCount; } /** * 获取手机上所有的App * * @param context * @return */ public static List<PackageInfo> getAllAppList(Context context) { PackageManager pkgMgr = context.getPackageManager(); List<PackageInfo> apps = null; if (null != pkgMgr) { try { apps = pkgMgr.getInstalledPackages(0); } catch (Exception ignored) { } } return apps; } /** * 获取手机上所有的非系统App * * @param context * @return */ public static List<PackageInfo> getUserAppList(Context context) { PackageManager pkgMgr = context.getPackageManager(); List<PackageInfo> apps = null; if (null != pkgMgr) { try { apps = pkgMgr.getInstalledPackages(0); } catch (Exception ignored) { } } List<PackageInfo> userAppList = null; if (null != apps && apps.size() > 0) { userAppList = new ArrayList<>(); for (int i = 0; i < apps.size(); i++) { if (!isSysApp(apps.get(i))) { userAppList.add(apps.get(i)); } } } return userAppList; } /** * 获取签名的MD5 * return 签名的md5 */ public static String getSignMd5Str(String pkgName) { byte[] sign = getSignInfo(pkgName); if (null == sign || sign.length == 0) { return null; } String signMd5 = null; try { signMd5 = Md5Util.getMD5String(sign); } catch (Exception e) { LogUtil.e(e); } return signMd5; } /** * 获取App签名 */ public static byte[] getSignInfo(String pkgName) { if (TextUtils.isEmpty(pkgName)) { return null; } try { @SuppressLint("PackageManagerGetSignatures") PackageInfo packageInfo = OlemobApplication.getInstance() .getPackageManager().getPackageInfo(pkgName, PackageManager.GET_SIGNATURES); Signature[] signs = packageInfo.signatures; Signature sign = signs[0]; return sign.toByteArray(); } catch (Exception e) { LogUtil.e(e); } return null; } /** * 该方法耗时0.5秒,请注意在子线程下运行 * * @param pkgName */ public static String getApkMd5(String pkgName) { try { String apkPaths = getExecResult("pm path " + pkgName); if (TextUtils.isEmpty(apkPaths)) { return ""; } String path = apkPaths; String[] pathArray = apkPaths.split(":"); if (pathArray.length >= 2) { path = pathArray[1]; } String md5String = getExecResult("md5 " + path); if (!TextUtils.isEmpty(md5String) && md5String.contains("/")) { md5String = md5String.substring(0, md5String.indexOf("/")).trim(); } return md5String; } catch (Exception ignored) { } return ""; } private static String getExecResult(String argsString) { Process process = null; DataOutputStream dataOutputStream = null; try { process = Runtime.getRuntime().exec(argsString); dataOutputStream = new DataOutputStream(process.getOutputStream()); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); final StringBuilder log = new StringBuilder(); boolean firstLine = true; String line; while ((line = reader.readLine()) != null) { if (!firstLine) { log.append("*#*"); } else { firstLine = false; } log.append(line); } return log.toString(); } catch (Exception ignored) { } finally { try { if (dataOutputStream != null) dataOutputStream.close(); if (process != null) process.destroy(); } catch (Exception ignored) { } } return ""; } public static void gotoMarket(Context context, String pkgName) { if (context == null || TextUtils.isEmpty(pkgName)) { return; } try { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + pkgName)); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } catch (Exception e) { LogUtil.e(e); } } }
最后
以上就是欢喜音响为你收集整理的获取已安装APP的信息的全部内容,希望文章能够帮你解决获取已安装APP的信息所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复