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" />