Check if the application is installed - Android

I am trying to install applications from Google Play. I can understand that when I open the URL of the Google Play store, Google Play opens, and when I click the back button, activity resumes.

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

When I returned to this activity, I tried calling this onResume() to check if the application is installed, but I get an error message:

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

The error is as follows:

E / AndroidRuntime (796): java.lang.RuntimeException: cannot start Events ComponentInfo {com.example.appinstaller / com.example.appinstaller.MainActivity}: android.content.ActivityNotFoundException: No activity found for operation Intent {act = android. intent.action.VIEW dat = market: // details? id = com.package.name flg = 0x40080000}

I assume the activity is onPause() . Is there a better way to implement it? I am trying to check if the application is completed.

+81
android android-intent google-play
Sep 11 '13 at 22:15
source share
5 answers

Try the following:

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

It tries to get information about the package whose name you passed. Otherwise, if a NameNotFoundException was thrown, it means that a package with that name was not installed, so we return false .

Please note that we pass the PackageManager instead of Context , so the method is somewhat more flexible and does not violate the law of Demeter . You can use this method without access to the Context instance if you have an instance of PackageManager .

Use it as follows:

 public void someMethod() { // ... PackageManager pm = context.getPackageManager(); boolean isInstalled = isPackageInstalled("com.somepackage.name", pm); // ... } 
+258
Sep 11 '13 at 22:20
source share

Robin Kanter's answer is right, but he checks installed applications regardless of their enabled or disabled state.

We all know that the application can be installed, but disabled by the user, therefore unsuitable for use.

This checks for installed applications AND :

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

You can put this method in a class like Utils and call it everywhere using:

 boolean isInstalled = Utils.isPackageInstalled("com.package.name", context.getPackageManager()) 
+20
Mar 07 '17 at 11:41
source share

Try the following:

 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; } 
+5
Dec 31 '15 at 6:16
source share

Faster solution:

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

getPackageGids cheaper from getPackageInfo , so it works faster.

 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 
+5
Sep 04 '17 at 22:17
source share
 @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; } 
-8
Jun 24. '15 at 2:45
source share



All Articles