Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening whatsapp through intent not working in Android 11

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();
        }
like image 759
John Avatar asked Nov 17 '20 09:11

John


People also ask

How can I send intent on Whatsapp?

Like most social apps on Android, WhatsApp listens to intents to share media and text. Simply create an intent to share text, for example, and WhatsApp will be displayed by the system picker: Intent sendIntent = new Intent(); sendIntent.

How do I launch an Android app from another app?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken button view to open YouTube application.


2 Answers

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

<queries>
    <package android:name="com.whatsapp" />
</queries>
like image 189
sdex Avatar answered Sep 21 '22 12:09

sdex


"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;
}
like image 21
sifr_dot_in Avatar answered Sep 19 '22 12:09

sifr_dot_in