Android Question Open WhatsApp?!!

Beja

Expert
Licensed User
Longtime User
Hi all,
Trying to invoke whatsapp but it didn't work, anyone has an idea why?
This is the code that I used. the B4A sub was generated by AI

[CODE lang="b4x" title="Open WhatsApp from B4A app"]Sub OpenWhatsApp
Try
Dim i As Intent
i.Initialize(i.ACTION_VIEW, "https://api.whatsapp.com")
i.SetPackage("com.whatsapp")
StartActivity(i)
Catch
ToastMessageShow("WhatsApp not Installed", True)
End Try

End Sub[/CODE]

Edit:
The required action is to open the WhatsApp App on the phnone, not the WhatsApp website URL
 
B4X:
Dim in As Intent
Dim pm As PackageManager
in = pm.GetApplicationIntent("com.whatsapp")
StartActivity (in)

You also need to add this in the Manifest
B4X:
AddManifestText(
<queries>
    <package android:name="com.whatsapp"/>
</queries>
)
 
Upvote 0
if you want only open whatsapp the source code above is perfect, but if you want also open a chat on a number and send a message:
Code:
    Dim i As Intent
    in.Initialize(in.ACTION_VIEW, $"whatsapp://send?phone=${Cell}&text=${Testo}"$)
    StartActivity(in)
where Cell is phone number (without "+") and Testo is the message "URLencoded UTF8"
 
Last edited:
Upvote 0
Back
Top