我如何找到一个特定的软件包或应用程序,比如: com.android.abc,是否存在于我的 Android 设备上?
com.android.abc
您应该使用名为 getInstalledPackages()的 PackageManager函数来获取所有已安装包的列表,并搜索您感兴趣的包。请注意,包名位于 PackageInfo.packageName字段中。
getInstalledPackages()
PackageManager
PackageInfo.packageName
使用包名称调用以下任何方法。
import android.content.pm.PackageManager; // ... public boolean isPackageExisted(String targetPackage){ List<ApplicationInfo> packages; PackageManager pm; pm = getPackageManager(); packages = pm.getInstalledApplications(0); for (ApplicationInfo packageInfo : packages) { if(packageInfo.packageName.equals(targetPackage)) return true; } return false; }
import android.content.pm.PackageManager; public boolean isPackageExisted(String targetPackage){ PackageManager pm=getPackageManager(); try { PackageInfo info=pm.getPackageInfo(targetPackage,PackageManager.GET_META_DATA); } catch (PackageManager.NameNotFoundException e) { return false; } return true; }
Intent intent = new Intent("com.google.zxing.client.android.SCAN"); PackageManager packageManager = getPackageManager(); List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0); boolean isIntentSafe = activities.size() > 0;
不使用 try-catch 块或迭代一堆包:
public static boolean isPackageInstalled(Context context, String packageName) { final PackageManager packageManager = context.getPackageManager(); Intent intent = packageManager.getLaunchIntentForPackage(packageName); if (intent == null) { return false; } List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); return list.size() > 0; }
由于一些设备报告说“ getInstalledPackages”可能导致 TransactionToLargeException (选中 这里、 这里和 这里) ,我认为您也应该像我下面所做的那样有一个备份。
这个问题本应该在 Android 5.1(阅读 这里)上得到解决,但仍有一些人报告了这个问题。
public static List<String> getInstalledPackages(final Context context) { List<String> result = new ArrayList<>(); final PackageManager pm = context.getPackageManager(); try { List<PackageInfo> apps = pm.getInstalledPackages(0); for (PackageInfo packageInfo : apps) result.add(packageInfo.packageName); return result; } catch (Exception ignored) { //we don't care why it didn't succeed. We'll do it using an alternative way instead } // use fallback: BufferedReader bufferedReader = null; try { Process process = Runtime.getRuntime().exec("pm list packages"); bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = bufferedReader.readLine()) != null) { final String packageName = line.substring(line.indexOf(':') + 1); result.add(packageName); } closeQuietly(bufferedReader); process.waitFor(); } catch (Exception e) { e.printStackTrace(); } finally { closeQuietly(bufferedReader); } return result; } public static void closeQuietly(final Closeable closeable) { if (closeable == null) return; try { closeable.close(); } catch (final IOException e) { } }
如果你只想使用 adb:
adb shell "pm list packages"|cut -f 2 -d ":"
它会列出所有已安装的软件包。
可以使用 pm.getPackageUid ()而不是迭代 pm.getInstalledApplications ()
boolean isPackageInstalled; PackageManager pm = getPackageManager(); int flags = 0; try { pm.getPackageUid(packageName,flags); isPackageInstalled = true; } catch (final PackageManager.NameNotFoundException nnfe) { isPackageInstalled = false; } return isPackageInstalled;
科特林
fun isPackageExist(context: Context, target: String): Boolean { return context.packageManager.getInstalledApplications(0).find { info -> info.packageName == target } != null }
编辑: 扩展功能
fun Context.isPackageExist(target: String): Boolean { return packageManager.getInstalledApplications(0).find { info -> info.packageName == target } != null }
我们可以这样检查:
if(getPackageManager().hasSystemFeature("android.software.webview") == true && isPackageExisted("com.google.android.webview")) { if (Constant.isNetworkConnected(Activity.this)) { //Your Intent } else { Toast.makeText(getApplicationContext(), resources.getString(R.string.internet_error), Toast.LENGTH_SHORT).show(); } } else { Constant.showDialog(Activity.this,"Please install the webview"); } }
制作方法的包裹检查! 这个信贷去“卡维”https://stackoverflow.com/a/30708227/6209105
public boolean isPackageExisted(String targetPackage) { List<ApplicationInfo> packages; PackageManager pm; pm = getPackageManager(); packages = pm.getInstalledApplications(0); for (ApplicationInfo packageInfo : packages) { if(packageInfo.packageName.equals(targetPackage)) { return true; } } return false;
}