I want to send a message to a particular contact on WhatsApp from my android app. I am able to do the same, but while sending it asks for opening with chrome and WhatsApp; I don't want the app to ask and instead directly open Whatsapp.
This opens specified contact on Whatsapp with specified phone number and pre-fills with the message.
What has to be written in here in place of the question mark for the className for example in case of opening facebook app, we write "sharingIntent.setClassName("com.facebook.katana", " com.facebook.katana.ShareLinkActivity ");"
You may send your intent to specific package by adding
intent.setPackage("com.whatsapp");
So you may just use same code which offeres to choose between chrome and WhatsApp, but adding the method above.
Try using Intent.EXTRA_TEXT instead of sms_body as your extra key. Per WhatsApp's documentation, this is what you have to use.
An example from their website :
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
Their example uses Intent.ACTION_SEND instead of Intent.ACTION_SENDTO, so I'm not sure if WhatsApp even supports sending directly to a contact via the intent system. Some quick testing should let you determine that.
try this :
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
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.