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);
}
Related
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"));
i already know the code how to send sms by typing the number and the message, but what i want to know is it possible to send message only without typing the contact number? Here’s my situation, im working at water district, I’m making app for reporting leak thru text, what i want to happen is, the reporter is only need to type the message then send, without inputting the number. We only have 1 hotline number, so I don’t think the reporter need to type the contact number everytime the reporter text us, he just need to type the message then click send only.
send sms without inputting contact number
Following code works without inputting the number again and again. The address attribute stores the number(hotline number) and When user clicks on report it default takes you to messaging app with number already stored and allow to write the text and go.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType("vnd.android-dir/mms-sms/");
intent.putExtra("address", your_hotline_number);
intent.putExtra("sms_body", " ");
startActivity(intent);
Try this it's working for me:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", "12345", null)));
Keep the recipient number in a constant field as
public static final String recipientNo = "*********";
Then if you use implicit intent the add below code
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + recipientNo));
intent.putExtra("sms_body", message);
startActivity(intent);
or if you use SmsManager,
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
and add permission in AndroidManifest.xml
<uses-permission android:name="android.permission.SEND_SMS"/>
For each product from my website there is a link SMS to send sms, PHONENUMBER is different from one product to another.how can I go through the link and save everything that appears after " sms: " ?
I want to save the phone number in a variable and set this with intend to be filled in sendto adress field,how can i do that?
ponumber=???; // this can not be predefine
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", ponumber);
smsIntent.putExtra("sms_body","Body of Message");
startActivity(smsIntent);
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.