I am using this code to call WhatsApp directly from my Call Logs app. This works well, but only if the phone number includes a valid country code. For example calling WhatsApp with 919789006685 works but calling it with 97890 06685 does not work. Here 91 is the country code.
Since my app reads phone numbers from Call Logs, I should be able to invoke WhatsApp with any valid contact phone number, irrespective of the stored phone number format. It is possible that users store numbers in their contacts which do not include country codes. So is there a workaround for this issue?
Code used within my app:
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
String waNumber = contactPhone.replace("+", "");
sendIntent.putExtra("jid", waNumber + "#s.whatsapp.net");
startActivity(sendIntent);
Here is the complete solution. May be useful for people who are trying the same from their apps. Thanks to Sourav Ganguly for the link.
android-make whatsapp call
Retreive the ID for the selected Contact.
Get all the RAW Contact Id's for the contact id from ContactsContract.RawContacts
Query the ContactsContract.Data table for all Raw Contacts and retrieve the column ContactsContract.Data._ID
Since a contact may have several phone numbers active on WhatsApp, you may want to check additionally the column ContactsContract.Data.DATA3 to filter out the phone number you want to match
To start a WhatsApp conversation use vnd.android.cursor.item/vnd.com.whatsapp.profile and vnd.android.cursor.item/vnd.com.whatsapp.voip.call if you want to start a WhatsApp call
Use the ID from step 3 to start WhatsApp. Here is sample code:
String data = "content://com.android.contacts/data/" + dataId;
String type = "vnd.android.cursor.item/vnd.com.whatsapp.profile";
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_VIEW);
sendIntent.setDataAndType(Uri.parse(data), type);
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
WhatsApp for an specific phone number
val whatsAppIntent = Intent(Intent.ACTION_VIEW)
val encodedText = URLEncoder.encode("Helo World", "UTF-8")
whatsAppIntent.data = Uri.parse("http://api.whatsapp.com/send?phone=$phoneNumber&text=$encodedText")
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/html");
sharingIntent.setPackage("com.whatsapp");
sharingIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("<p>https://play.google.com/store/apps/details?id=" + context.getPackageName() + "</p>"));
context.startActivity(Intent.createChooser(sharingIntent, "Share using"));
Related
I tried to search on Google but didn't find an appropriate answer.
I want to click a TextView and it will show a dialog that allows the user to choose optional email.
Any suggestion? Thanks in advance
Try this:
Intent intet = new Intent(Intent.ACTION_SEND);
intet.setType("message/rfc822");
intet.putExtra(android.content.Intent.EXTRA_TEXT, body);
startActivity(intet);
This will open up a dialog that contains a list of apps installed on the device that can send an email.
Hope this helps. :)
A better option is to use the following, it will only open the applications that provide mail facility and not all apps that can share data.
Intent emailIntent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:?subject=" + "subject" + "&body=" + "body" + "&to=" + email_id);
emailIntent.setData(data);
startActivity(emailIntent);
"subject" is the subject of mail, "body" is the content of the mail, and "email_id" is the id of the receiver of the mail.
You can keep the subject , body and email_id empty if you want the user to fill in the spaces.
for open gmail app
try this code:
Intent gmail = new Intent(Intent.ACTION_VIEW);
gmail.setClassName("com.google.android.gm","com.google.android.gm.ConversationListActivity");
startActivity(gmail);
I'm trying to find out whether a particular contact is a whatsapp user or not.
This goes my requirement:
I have a text to share and also have a phone number of a person as well. The priority of my sharing goes as follows
If the phone number is already present in CONTACTS and :
if the contact is a whatsapp user, I should call sharing intent of SEND with package set as "com.whatsapp"
i.e:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.setPackage("com.whatsapp");
intent.putExtra(Intent.EXTRA_TEXT, message);
startActivity(intent);
if the contact not a whatsapp user, I should call intent for default messaging app.
i.e :
Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( "sms:" + phoneNumber ) );
intent.putExtra( "sms_body", message );
startActivity(intent);
If the phone number is not present, create a contact and do as above.
My question now is, how do I find out whether my contact is a whatsapp user or not? Is whatsapp providing any API to find out that?
Thanks in advance
PS: I do not want to show a general sharing intent which shows a list of sharing apps. I may do that only if the device does not have whatsapp application at all.
how to send a whatsapp message to a specific contact automatically?I have searched but it just opens the chat of that specific person but does not send the message automatically
Here is the code that am using:
private void openWhatsappContact(String number) {
Uri uri = Uri.parse("smsto:" + number);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.setPackage("com.whatsapp");
startActivity(Intent.createChooser(i, ""));
I'm not sure you can send the message automatically, but try this to specify the message you want to send:
i.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
Reference:
https://www.whatsapp.com/faq/en/android/28000012
There is no direct way to do this because whatsapp does not offer an SDK interface.
You might have some luck with writing some accessibility automations or if you have root may be some xposed module.
I am writing an application that needs to pre-populate an SMS message with the first name of the contact when using the stock android SMS messenger.
For example if my contacts name is Alex Smith.
When I select Alex Smith to type a message to, I want the text box to already have
'Alex, '
at the beginning of the SMS.
Please how can I achieve that?
Fetch the contact name from contacts, and do like as follows:
String contactName = "Alex"; // get it from selected contact
Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( "sms:" + phoneNumber ) );
intent.putExtra( "sms_body", contactName+"," );
startActivity( intent );
Just look over here:
Send SMS via intent
You use a Intent to send the SMS and attach some pre defined text via the EXTRA_TEXT attribute. Quite easy to do. And it works for the normal SHARE intent as well.
You can't change system apps, but you can send "share intent" from your app to SMS App. As you remember, when you push "share" button in some apps, you can share smth with SMS. (You'll have SMS with text that app put in intent). SO, put contact's name in "share intent" and send it to SMS app.
it's bit late, but i hope it will help future users.
void prePopulateSMS(final String mblNumVar, final String smsMsgVar) {
Log.d(TAG, "prePopulateSMS called. smsMsgVar: " + smsMsgVar);
Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", mblNumVar);
smsIntent.putExtra("sms_body", smsMsgVar);
startActivity(smsIntent);
}
I am sort of new to android.
I am trying to send a msg via gmail and this is my code.
Intent intent = new Intent(Intent.ACTION_SENDTO);
String uriText;
uriText = "mailto:test#gmail.com?subject=Subject&body=this is test.";
uriText = uriText.replace(" ", "%20");
Uri uri = Uri.parse(uriText);
intent.setData(uri);
startActivity(Intent.createChooser(intent, "Select mailer"));
It works. But, before sending the msg, it shows a selection box
and asks me to choose either gmail or email.
How can I skip the selection box ?
Any help would be so appreciated.. Thanks.
How can I skip the selection box ?
You don't. You allow the user to choose what application they wish to use to send the message. Some users do not use Gmail.