-1

In android 11 how to check app is installed or not for particular package programmatically.

Divyesh Kevadiya
  • 57
  • 1
  • 2
  • 7

3 Answers3

6

Checking if app is installed having package name:

public static final String PACKAGE_NAME = "com.__.__";

    public static boolean isAppInstalled(Context context) {
        PackageManager packageManager = context.getPackageManager();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        if (intent.resolveActivity(packageManager) != null) {
            try {
                packageManager.getPackageInfo(PACKAGE_NAME, PackageManager.GET_ACTIVITIES);
                return true;
            } catch (PackageManager.NameNotFoundException ignore) {

            }
        }
        return false;
    }

EDITED:

Add to Manifest for Android 11:

 <queries>
        <package android:name="com.___.___" />
 </queries>
Marina
  • 149
  • 8
1

have to add this permissions in your android manifest first.

<permission android:name="android.permission.QUERY_ALL_PACKAGES" />
<queries>
<intent>
<action android:name="android.intent.action.MAIN" />
</intent>
</queries>

then call this methods to check particular app is installed or all installed app list.

  public static boolean appInstalledOrNot(Context context, String str) {
    try {
        context.getPackageManager().getPackageInfo(str, 1);
        return true;
    } catch (PackageManager.NameNotFoundException unused) {
        return false;
    }
}

public static boolean isAppInstalled1(Context context) {
    PackageManager packageManager = context.getPackageManager();
    Intent intent = new Intent(Intent.ACTION_VIEW);
    if (intent.resolveActivity(packageManager) != null) {
        try {
            packageManager.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException ignore) {

        }
    }
    return false;
}

private boolean getAllAPps() {
    PackageManager packageManager = getApplicationContext().getPackageManager();
    for (PackageInfo packageInfo : packageManager.getInstalledPackages(0)) {
        Log.e(TAG, "getAllAPps: " + packageInfo.packageName);
        if (packageInfo.packageName.equals("com.whatsapp")) {
            return true;
        }
    }
    return false;
}
Divyesh Kevadiya
  • 57
  • 1
  • 2
  • 7
0

It is easy, first add the following function:

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

now add the following code whenever you want to check for installed app:

    String packagename = "com.test.app";//replace the string with your app package name you want to check
    PackageManager pm = getPackageManager();
    boolean app_is_installed = isPackageInstalled(packagename, pm);
    if(app_is_installed)
    {
      //do something
    }
farhad.kargaran
  • 2,103
  • 1
  • 22
  • 29