How to check Whatsapp is installed in device in android?

This is the code to get all package names of installed applications in device

 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    final List pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);

after getting list of packages search for com.whatsapp(package name of whats app given on official webiste Whatsapp). Thats it..


based on @eliasz-kubala answer this will work for me on Android 11 after only adding this to Manifest

  <manifest 
    ...> 
    <queries> <package android:name="com.whatsapp" /> </queries>
  
    <application ....>
    </application>
  </manifest>

and Then use this Kotlin function

  private fun isAppInstalled(packageName: String): Boolean {
    val pm: PackageManager = getPackageManager()
    return try {
        pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
        true
    } catch (e: PackageManager.NameNotFoundException) {
        false
    }
   }

You can use this code. It will check if package is installed.

public class Example extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //Put the package name here...
        if(isAppInstalled("com.whatsapp")) {
            System.out.println("App is already installed on your phone");         
        } else {
            System.out.println("App is not currently installed on your phone");
        }
    }

    private boolean isAppInstalled(String packageName) {
        try {
            getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
            return true;
        }
        catch (PackageManager.NameNotFoundException ignored) {
            return false;
        }
    }
}

Tags:

Android