Android 获取未安装的APK图标、版本号、包名等信息方法
一、获取APK图标
public static Drawable getApkIcon(Context context, String apkPath) { PackageManager pm = context.getPackageManager(); PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES); if (info != null) { ApplicationInfo appInfo = info.applicationInfo; appInfo.sourceDir = apkPath; appInfo.publicSourceDir = apkPath; try { return appInfo.loadIcon(pm); } catch (OutOfMemoryError e) { Log.e("ApkIconLoader", e.toString()); } } return null; }
下面代码段中PackageManager、PackageInfo、ApplicationInfo均同上面一致。
二、获取APK名称
String label = appInfo.loadLabel(mPackManager).toString();
三、获取APK包名
String packageName = appInfo.packageName;
四、获取APK版本号
String version = info.versionName==null?<p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px;"></p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px;">"0":info.versionName</p>
五、推断APK是否安装
private boolean isApkInstalled(String packagename) { PackageManager localPackageManager = getPackageManager(); try { PackageInfo localPackageInfo = localPackageManager.getPackageInfo(packagename, PackageManager.GET_UNINSTALLED_PACKAGES); return true; } catch (PackageManager.NameNotFoundException localNameNotFoundException) { return false; } }
六、安装APK
private void installAPK(String apkPath) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.parse("file://" + apkPath), "application/vnd.android.package-archive"); mContext.startActivity(intent); }
七、打开APK
private void openAPK(String packagename) { PackageManager packageManager = mContext.getPackageManager(); Intent intent=new Intent(); intent =packageManager.getLaunchIntentForPackage(packagename); mContext.startActivity(intent); }
以上这篇Android 获取未安装的APK图标、版本号、包名等信息方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。
您可能感兴趣的文章:
- Android中获取apk安装包信息的方法
- Android编程获取包名,版本信息及VersionName名称的方法
赞 (0)