How to check programmatically if an application is installed or not in Android?

后端 未结 15 1066
悲哀的现实
悲哀的现实 2020-11-22 05:51

We have installed applications programmatically.

  1. If the application is already installed in the device the application is open automatically.
  2. Otherwis
相关标签:
15条回答
  • 2020-11-22 06:30

    So nicer with Kotlin suger:

      private fun isSomePackageInstalled(context: Context, packageName: String): Boolean {
    
        val packageManager = context.packageManager
    
        return runCatching { packageManager.getPackageInfo(packageName, 0) }.isSuccess
      }
    
    0 讨论(0)
  • 2020-11-22 06:31

    Check App is installed or not in Android by using kotlin.

    Creating kotlin extension.

    fun PackageManager.isAppInstalled(packageName: String): Boolean = try {
            getApplicationInfo(packageName, PackageManager.GET_META_DATA)
            true
        } catch (e: Exception) {
            false
        }
    

    Now, can check if app is install or not

    if (packageManager.isAppInstalled("AppPackageName")) {
        // App is installed
    }else{
        // App is not installed
    }
    
    0 讨论(0)
  • 2020-11-22 06:31

    Try this

    This code is used to check weather your application with package name is installed or not if not then it will open playstore link of your app otherwise your installed app

    String your_apppackagename="com.app.testing";
        PackageManager packageManager = getPackageManager();
        ApplicationInfo applicationInfo = null;
        try {
            applicationInfo = packageManager.getApplicationInfo(your_apppackagename, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (applicationInfo == null) {
            // not installed it will open your app directly on playstore
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + your_apppackagename)));
        } else {
            // Installed
            Intent LaunchIntent = packageManager.getLaunchIntentForPackage(your_apppackagename);
            startActivity( LaunchIntent );
        }
    
    0 讨论(0)
  • 2020-11-22 06:32

    The above code didn't work for me. The following approach worked.

    Create an Intent object with appropriate info and then check if the Intent is callable or not using the following function:

    private boolean isCallable(Intent intent) {  
            List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent,   
            PackageManager.MATCH_DEFAULT_ONLY);  
            return list.size() > 0;  
    }
    
    0 讨论(0)
  • 2020-11-22 06:40

    I think using try/catch pattern is not very well for performance. I advice to use this:

    public static boolean appInstalledOrNot(Context context, String uri) {
        PackageManager pm = context.getPackageManager();
        List<PackageInfo> packageInfoList = pm.getInstalledPackages(PackageManager.GET_ACTIVITIES);
        if (packageInfoList != null) {
            for (PackageInfo packageInfo : packageInfoList) {
                String packageName = packageInfo.packageName;
                if (packageName != null && packageName.equals(uri)) {
                    return true;
                }
            }
        }
        return false;
    }
    
    0 讨论(0)
  • 2020-11-22 06:43

    All the answers only check certain app is installed or not. But, as we all know an app can be installed but disabled by the user, unusable.

    Therefore, this solution checks for both. i.e, 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;
         }
    }
    

    Call the method isPackageInstalled():

    boolean isAppInstalled = isPackageInstalled("com.android.app" , this.getPackageManager());
    

    Now, use the boolean variable isAppInstalled and do whatever you want.

    if(isAppInstalled ) {
        /* do whatever you want */
    }
    
    0 讨论(0)
提交回复
热议问题