检查应用程序是否安装 – Android

我正尝试从Google Play安装应用程序。 我可以理解,在打开Goog​​le Play商店url时,会打开Goog​​le Play,当我按下后退button时,活动将继续。

Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(appURL)); marketIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); startActivity(marketIntent); 

当我回到活动,我试图调用这个onResume()来检查应用程序是否安装,但我收到一个错误:

 @Override protected void onResume() { super.onResume(); boolean installed = false; while (!installed) { installed = appInstalledOrNot(APPPACKAGE); if (installed) { Toast.makeText(this, "App installed", Toast.LENGTH_SHORT).show(); } } } private boolean appInstalledOrNot(String uri) { PackageManager pm = getPackageManager(); boolean app_installed = false; try { pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); app_installed = true; } catch (PackageManager.NameNotFoundException e) { app_installed = false; } return app_installed ; } 

错误如下:

E / AndroidRuntime(796):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.appinstaller / com.example.appinstaller.MainActivity}:android.content.ActivityNotFoundException:找不到处理Intent的动作{act = android .intent.action.VIEW dat = market:// details?id = com.package.name flg = 0x40080000}

我猜测活动是onPause() 。 有没有更好的方法来实现它? 我试图检查应用程序是否已完成安装。

尝试这个:

 private boolean isPackageInstalled(String packagename, PackageManager packageManager) { try { packageManager.getPackageInfo(packagename, 0); return true; } catch (NameNotFoundException e) { return false; } } 

它试图获取关于你传入的名字的包的信息。如果一个NameNotFoundExceptionexception被抛出,那就意味着没有安装这个名字的包,所以我们返回false

请注意,我们传递一个PackageManager而不是一个Context ,所以这个方法稍微灵活一些,不违反Demeter的规律 。 只要有一个PackageManager实例,就可以使用该方法而不访问Context实例。

像这样使用它:

 public void someMethod() { // ... PackageManager pm = context.getPackageManager(); boolean isInstalled = isPackageInstalled("com.somepackage.name", pm); // ... } 

尝试这个:

 public static boolean isAvailable(Context ctx, Intent intent) { final PackageManager mgr = ctx.getPackageManager(); List<ResolveInfo> list = mgr.queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY); return list.size() > 0; } 

Robin Kanters的回答是正确的,但它确实检查已安装的应用程序,而不pipe其启用或禁用状态。

我们都知道一个应用程序可以安装,但用户禁用,因此无法使用。

这检查已安装启用的应用程序:

 public static boolean isPackageInstalled(String packageName, PackageManager packageManager) { try { return packageManager.getApplicationInfo(packageName, 0).enabled; } catch (PackageManager.NameNotFoundException e) { return false; } } 

你可以把这个方法放在像Utils这样的类中,并在任何地方使用:

 boolean isInstalled = Utils.isPackageInstalled("com.package.name", context.getPackageManager()) 

更快的解决scheme:

 private boolean isPackageInstalled(String packagename, PackageManager packageManager) { try { packageManager.getPackageGids(packagename); return true; } catch (NameNotFoundException e) { return false; } } 

getPackageGids便宜,所以它工作得更快。

 Run 10000 on API 15 Exists pkg: getPackageInfo: nanoTime = 930000000 getPackageGids: nanoTime = 350000000 Not exists pkg: getPackageInfo: nanoTime = 420000000 getPackageGids: nanoTime = 380000000 Run 10000 on API 17 Exists pkg: getPackageInfo: nanoTime = 2942745517 getPackageGids: nanoTime = 2443716170 Not exists pkg: getPackageInfo: nanoTime = 2467565849 getPackageGids: nanoTime = 2479833890 Run 10000 on API 22 Exists pkg: getPackageInfo: nanoTime = 4596551615 getPackageGids: nanoTime = 1864970154 Not exists pkg: getPackageInfo: nanoTime = 3830033616 getPackageGids: nanoTime = 3789230769 Run 10000 on API 25 Exists pkg: getPackageInfo: nanoTime = 3436647394 getPackageGids: nanoTime = 2876970397 Not exists pkg: getPackageInfo: nanoTime = 3252946114 getPackageGids: nanoTime = 3117544269 
 @Override protected void onResume() { super.onResume(); boolean installed = false; while (!installed) { installed = appInstalledOrNot (APPPACKAGE); if (installed) { Toast.makeText(this, "App installed", Toast.LENGTH_SHORT).show (); } } } private boolean appInstalledOrNot (String uri) { PackageManager pm = getPackageManager(); boolean app_installed = false; try { pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); app_installed = true; } catch (PackageManager.NameNotFoundException e) { app_installed = false; } return app_installed; }