Im writing an app that sends an SMS to several contacts. The contacts numbers are stored in an ArrayList (was received from another activity). I am not able to use this ArrayList to pass several contacts to the built-in SMS android app. This is the code:
ArrayList<String> numbersArrayList=getIntent().getExtras().getStringArrayList("phoneNumbers");
String message= "this is a custom message";
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.putExtra("sms_body", message);
smsIntent.putExtra("address", ??????????);
smsIntent.setType("vnd.android-dir/mms-sms");
startActivity(smsIntent);
I can iterate and print these contacts to the LogCat the simple "for each" loop and overriding toString method.
Use this code..
String toNumbers = "";
for ( String s : numbersArrayList)
{
toNumbers = toNumbers + s + ";"
}
toNumbers = toNumbers.subString(0, toNumbers.length - 1);
String message= "this is a custom message";
Uri sendSmsTo = Uri.parse("smsto:" + toNumbers);
Intent intent = new Intent(
android.content.Intent.ACTION_SENDTO, sendSmsTo);
intent.putExtra("sms_body", message);
startActivity(intent);
Related
I found multiple threads here about this, but none of them are working for me. This is my code:
public void sendSMS (String number, String body) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("smsto:" + number));
i.putExtra("abc", body);
startActivity(i);
}
It opens stock SMS app and passes the number to send, but text body is empty.
I also tried this Uri.fromParts(body, number, null) but then the app just crashes.
Replace your code with below code. You are not passing correct keys to detect number and text body for the sms.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + phoneNumber));
intent.putExtra("sms_body", message);
startActivity(intent);
Please read this documentation for detailed information.
Compose an SMS/MMS message with attachment
Try this
Intent smsIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + phoneNumber));
smsIntent.putExtra("address", "12125551212");
smsIntent.putExtra("sms_body","Body of Message");
startActivity(smsIntent);
If you want to ensure that your intent is handled only by a text messaging app (and not other email or social apps), then use the ACTION_SENDTO action and include the "smsto:" data scheme. For example:
public void composeMmsMessage(String message, Uri attachment) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setData(Uri.parse("smsto:")); // This ensures only SMS apps respond
intent.putExtra("sms_body", message);
intent.putExtra(Intent.EXTRA_STREAM, attachment);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
I'm trying to send a text message from my android application to Whatsapp to specific Contact. when I'm using below codes, I am succeed either to send message and have to pickup contact manually, or If Specific number chat window opens, but message is blank. So is it possible to do both with one intent ? Here is my code:
I can share message to WhatsApp, but contact i have to choose manually:
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.setPackage("com.whatsapp");
i.putExtra(Intent.EXTRA_TEXT, "Hello World");
try {
activity.startActivity(i);
} catch (Exception e) {
e.printStackTrace();
}
Specific number in wats app window opens, but message is blank:
Uri uri = Uri.parse("smsto:" + number);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra("sms_body", "smsText");
i.setPackage("com.whatsapp");
activity.startActivity(i);
Below code will helps you :
String strMessageToShare=YourEditText.getText().tostring();
final Intent myIntent = new Intent(
android.content.Intent.ACTION_SEND_MULTIPLE);
myIntent.setType("text/plain");
myIntent.putExtra(android.content.Intent.EXTRA_TEXT,
new String[]{strMessageToShare});
YourActivity.this.startActivityForResult(Intent
.createChooser(myIntent, "Sharing message..."), 1);
This is what works for me.
The parameter 'body' gets not read by the whatsapp app, use 'Intent.EXTRA_TEXT' instead.
By setting the 'phoneNumber' you specify the contact to open in whatsapp.
Intent sendIntent = new Intent(Intent.ACTION_SENDTO,
Uri.parse("smsto:" + "" + phoneNumber + "?body=" + encodedMessage));
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
I'm wondering if I can send text data from my Android application to WhatsApp without showing the contacts list of WhatsApp, I just need to set the message and the mobile number internally in my Android code?
You can try something like this:
public void sendWhatsappMessageToMsisdn(String contactNumber)
{
Uri uri = Uri.parse("smsto:" + contactNumber);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.setPackage("com.whatsapp");
intent.putExtra(Intent.EXTRA_TEXT, "This is the message you want to send");
startActivity(Intent.createChooser(intent, ""));
}
Here I am trying to send the message hello world to a number.
Here is My code snippet:
public void openWhatsappContact(String number) {
Uri uri = Uri.parse("smsto:" + number);
Intent i = new Intent(Intent.ACTION_SENDTO,uri);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, "Hello World");
i.putExtra("chat",true);
i.setPackage("com.whatsapp");
startActivity(Intent.createChooser(i, ""));
}
But its showing error:
No applications can perform this action
I registered my receiver to get SMS. When I receive SMS's, how can I execute the phone's default SMS app?
Can I use the intent send action to start the default SMS app?
It can be done in a couple of different ways. Here's one:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "Content of the SMS goes here...");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
Here "number" is an array of strings with the numbers of contacts to whom you want to send sms to and "älldetails" is teh string you want to send.
String n = "";
for(int i = 0; i<sizesf ;i++)
{
if(i == (sizesf-1))
{
n = n + number[i];
}
else
n = n + number[i] + ";";
}
Log.d("numbers in intent", n);
Intent smsIntent = new Intent( Intent.ACTION_VIEW, Uri.parse( "smsto:"+ n) );
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", n );
smsIntent.putExtra("sms_body",alldetails);
startActivity(smsIntent);
}