Check if application is installed – Android

I’m trying to install apps from Google Play. I can understand that on opening the Google Play store URL, it opens the Google Play and when I press the back button, the 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 went back to the activity, I tried calling this onResume() to check if the app is installed, but I receive an error:

  • Vibrate onclick
  • Encryption using AES-128 in Android and IPhone (Different result)
  • Disable notifications on a ContentProvider URI
  • When to use CheckBox and when Switch
  • Firebase Crash Library - NullPointerException in the console
  • How to use Path (android.graphics.Path) with canvas in android?
  • @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: Unable to start
    activity
    ComponentInfo{com.example.appinstaller/com.example.appinstaller.MainActivity}:
    android.content.ActivityNotFoundException:
    No Activity found to handle Intent { act=android.intent.action.VIEW
    dat=market://details?id=com.package.name flg=0x40080000 }

    I guess the activity is onPause(). Is there a better way to implement it? I’m trying to check if the app has finished installing.

    Related posts:

    android list files contained in assets subfolder
    Android application restarts when opened by clicking the application icon
    Difference between Locale FRENCH vs Locale FRANCE
    How to check which notifications are active in status bar in Android Dev?
    Adding Table rows Dynamically in Android
    indeterminateProgressBar in ActionBar styling - padding issue
  • Android Eclipse Plugin “reset adb” gives “Adb connection Error:EOF” error, not blocking, but what is it?
  • Retrofit 2 with only form-data
  • How to put/get multiple JSONObjects to JSONArray?
  • Implement startForeground method in Android
  • get bitmap from visible zoomed image android
  • Upload live android webcam video to RTP/RTSP Server
  • 4 Solutions collect form web for “Check if application is installed – Android”

    Try this:

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

    It attempts to fetch information about the package whose name you passed in. Failing that, if a NameNotFoundException was thrown, it means that no package with that name is installed, so we return false.

    Note that we pass in a PackageManager instead of a Context, so that the method is slightly more flexibly usable and doesn’t violate the law of Demeter. You can use the method without access to a Context instance, as long as you have a PackageManager instance.

    Use it like this:

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

    Try this:

    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’ answer is right, but it does check for installed apps regardless of their enabled or disabled state.

    We all know an app can be installed but disabled by the user, therefore unusable.

    This checks for installed AND enabled apps:

    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())
    
    @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;
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.