本文主要是介绍Android 获取已安装应用、包名、应用名、版本号、版本名,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、相关代码
List<ApplicationInfo> installedApps = getPackageManager().getInstalledApplications(0);for (ApplicationInfo appInfo : installedApps) {CharSequence getAppName = getPackageManager().getApplicationLabel(appInfo);String appName=getAppName.toString();//获取到app名String packageName=appInfo.packageName;String versionName=getVersionName(this,packageName);//一般这个就是版本号--1.2.11Log.d(">>>appInfo ","appName="+appName+",,,,,,,packageName="+packageName+",,,,,,,,,versionCode="+versionCode+",,,,,,,,,,versionName="+versionName);if (packageName.equals("com.mywl.heiangnscxb")){// 应用程序已安装break;}}public int getVersionCode(Context context,String packageName) {PackageManager manager = context.getPackageManager();int code = 0;try {PackageInfo info = manager.getPackageInfo(packageName, 0);code = info.versionCode;} catch (PackageManager.NameNotFoundException e) {e.printStackTrace();}return code;}public String getVersionName(Context context,String packageName) {PackageManager manager = context.getPackageManager();String name = null;try {PackageInfo info = manager.getPackageInfo(packageName, 0);name = info.versionName;} catch (PackageManager.NameNotFoundException e) {e.printStackTrace();}return name;}
2、相关权限
加上权限才能获取到所有的应用
<uses-permissionandroid:name="android.permission.QUERY_ALL_PACKAGES"tools:ignore="QueryAllPackagesPermission" />
这篇关于Android 获取已安装应用、包名、应用名、版本号、版本名的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!