简体   繁体   中英

In Android how to check app is installed in your phone or not android 11 programmatically

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

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>

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

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
    }

try to add this line in AndroidManifest this code is worked in android 11 and 12 to detect that if app is installed or not

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

and your activity add below code for checking

if (appInstalledOrNot("com.whatsapp")) {
                    Intent intent = new Intent(getApplicationContext(), WhatsappActivity.class);
                    intent.putExtra("which", "WA");
                    startActivity(intent);
                    return;
                }
                Toast.makeText(getApplicationContext(), getResources().getString(R.string.noInstall), 0).show();
            

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM