第一种方法:
public static boolean checkInstalled(Context context, String pkgName) { if (TextUtils.isEmpty(pkgName)) { return false; } try { context.getPackageManager().getPackageInfo(pkgName, 0); } catch (Exception x) { return false; } return true; }
第二种方法:
public boolean checkApkExist(Context context, String packageName){ PackageManager manager = context.getPackageManager(); ListpkgList = manager.getInstalledPackages(0); for (int i = 0; i < pkgList.size(); i++) { PackageInfo pI = pkgList.get(i); Log.d("Test","++++pI.packageName:"+pI.packageName); if (pI.packageName.equalsIgnoreCase(packageName)){ return true; } } return false; }
注意事项:
api29以上检测其他应用是否安装需要添加权限
- <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
- tools:ignore="QueryAllPackagesPermission" />
否则无法获取应用的安装情况。