如何才能find我的Android设备上是否存在特定的软件包?

我怎样才能find一个特定的包或应用程序,说: com.android.abc ,我的Android设备上存在?

使用包名称调用以下任何方法。

 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; } 

您应该使用PackageManager的函数getInstalledPackages()来获取所有已安装软件包的列表,并search您感兴趣的软件包。请注意,软件包名称位于PackageInfo.packageName字段中。

 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”可能导致TransactionTooLargeException(请点击这里在这里这里 ),我认为你也应该有一个像以下那样的后备。

这个问题应该是固定在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 ":" 

它会列出所有已安装的软件包。