17

Opening Whatsapp with intent is not working in android OS 11 but working fine up to android (OS) 10 devices, It displays the message "Whatsapp app not installed in your phone" on the android 11 device. Does anyone have a solution for this?

String contact = "+91 9999999999"; // use country code with your phone number
        String url = "https://api.whatsapp.com/send?phone=" + contact;
        try {
            PackageManager pm = context.getPackageManager();
            pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES);
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            context.startActivity(i);
        } catch (PackageManager.NameNotFoundException e) {
          Toast.makeText(mContext, "Whatsapp app not installed in your phone",Toast.LENGTH_LONG).show();
           e.printStackTrace();
        }
John
  • 191
  • 1
  • 1
  • 6

5 Answers5

39

There are new changes in android 11 of package visibility.
You need to add a new section queries under you app's <manifest> tag with desired package name:

<manifest package="com.example.app">
    <queries>
        <package android:name="com.whatsapp" />
    </queries>
  ...
</manifest>
sdex
  • 3,169
  • 15
  • 28
  • Thanks for your help. I have added the package name as above in the manifest file but the Gradle build is failed and it shows an error as Execution failed for task ':app:processDebugManifest'. > Manifest merger failed with multiple errors, see logs... – John Nov 17 '20 at 12:25
  • Please share your manifest and also your config - target sdk, build tools, android studio, and gradle plugin version. – sdex Nov 17 '20 at 12:53
  • 1
    by adding in manifest file, it works – John Nov 19 '20 at 07:01
  • It may look suspicious for the users. You need to query just one package, so it's more correct to use the new `queries` option in the manifest file. – sdex Nov 19 '20 at 07:28
  • Do not add this line of permissions can work normally, but after adding it will not work – Zhuoyuan.Li Dec 02 '20 at 12:06
  • 1
    Thanks for the help...this solution worked for me Edit: for people not working try inserting this exact same block just above the application tag inside the manifest file and it should work fine – Shubham Shah Sep 30 '21 at 07:53
6

Instead of using wildcards, it's more explicit to add both package names:

<manifest package="com.example.app">
    <queries>
        <package android:name="com.whatsapp"/>
        <package android:name="com.whatsapp.w4b"/>
    </queries>
  ...
</manifest>
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
5

"com.whatsapp"
can also be the culprit.

i was also boggled with this message.

the issue was "whatsApp business app" which has package name:
"com.whatsapp.w4b"

used following code to find out which one is installed:

String appPackage="";
if (isAppInstalled(ctx, "com.whatsapp.w4b")) {
    appPackage = "com.whatsapp.w4b";
    //do ...
} else if (isAppInstalled(ctx, "com.whatsapp")) {
    appPackage = "com.whatsapp";
    //do ...
} else {
    Toast.makeText(ctx, "whatsApp is not installed", Toast.LENGTH_LONG).show();
}

private boolean isAppInstalled(Context ctx, String packageName) {
    PackageManager pm = ctx.getPackageManager();
    boolean app_installed;
    try {
        pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        app_installed = true;
    } catch (PackageManager.NameNotFoundException e) {
        app_installed = false;
    }
    return app_installed;
}
sifr_dot_in
  • 3,153
  • 2
  • 33
  • 42
0

Rather than adding each Package names in , you can add:

**<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" tools:ignore="QueryAllPackagesPermission" />**

to your AndroidManifest.xml file in your project. I was also bothering with the same, this permission allowed me to troubleshoot my issue/error.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Harsh Rana
  • 39
  • 3
0

To open Whatsapp or Whatsapp Business on button click use the below code.

To open whatsapp

Intent intent1 = getPackageManager().getLaunchIntentForPackage("com.whatsapp");
startActivity(intent1);
            

To open Business whatsapp

Intent intent2 = getPackageManager().getLaunchIntentForPackage("com.whatsapp.w4b");
startActivity(intent2);
            
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81