Android SMS intent exit_on_sent removes message body - android

I realize this concern has been expressed before, here and here, but it has never been addressed in the responses (and to be honest has not been the main focus of the question).
I am NOT asking how to return back after using an SMS intent in Android. I am asking how to enable exit_on_sent without the side-effect of wiping sms_body.
This is my code:
Intent msg = new Intent(Intent.ACTION_VIEW);
msg.setData(Uri.parse("smsto:01234567899"));
msg.putExtra("sms_body", "\n\nSent from my very own App!");
//msg.putExtra("exit_on_sent", true);
startActivity(msg);
It currently works with the message body I supply with msg.putExtra(), but does not return back afterwards as the following line is commented out.
But when I uncomment the line which sets exit_on_sent to true, although the return back functionality now works, the message body is no longer in the SMS app when it opens, nor in the message when viewed after sending.
Thank you for your time.

For the exit_on_sent to work in conjunction with sms_body you will need to use getDefaultSmsPackage as per below:
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "sms body");
intent.putExtra("exit_on_sent", true);
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(ctx);
if (defaultSmsPackageName != null)
intent.setPackage(defaultSmsPackageName);
} else {
intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setType("vnd.android-dir/mms-sms");
intent.putExtra("sms_body", "sms body");
}
act.startActivity(intent);

Related

Pass sms body to default sms app

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);
}
}

Send SMS to multiple phone number when messenger is set as default sms app in android

I have implemented the ability to send message in my app and it is working well. But if the user is using another sms app like messenger as their default sms app then, I can't send message to multiple recipients.
If multiple phone number is selected, only one of them get the message in most cases the last phone number.
NOTE: I'm using an implicit intent to send the message and it can send to multiple recipients on stock sms app.
Any help will ve greatly appreciated.
This is what I have as requested
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
String defaultSmsPackage = Telephony.Sms.getDefaultSmsPackage(getActivity());
if (defaultSmsPackage != null) {
intent.setPackage(defaultSmsPackage);
}
} else {
Uri numbersUri = Uri.parse("tel:" + phoneNumbers);
intent = new Intent(Intent.ACTION_VIEW, numbersUri);
intent.setType("vnd.android-dir/mms-sms");
}
intent.putExtra("address", phoneNumbers);
intent.putExtra("sms_body", message);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivity(intent);
}

Android: sending SMS through intent with body and returning back.

I am trying to send a SMS through an intent, I want to add a body to the message. After user press send I want to return to the app. I've added extra as sms_body and exit_on_sent. But when I use them both the SMS appears without the body. If i don't use the exit_on_sent extra everything works fine.
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("smsto:" + phoneNumber));
sendIntent.putExtra("sms_body", "some text");
sendIntent.putExtra("exit_on_sent", true);
context.startActivity(sendIntent);
You could try using
startActivityForResult(sendIntent, SOME_REQUEST_CODE)
but in my experience it doesn't works most of the time.
I would recommend instead using SmsManager.
SmsManager smsMgr = SmsManager.getDefault();
if(smsMgr != null){
PendingIntent sentIntent = PendingIntent.getBroadcast(
getActivity().getApplicationContext(), 0,
new Intent(MY_ACTION_INTENT_SENT), 0);
smsMgr.sendTextMessage(phone, null, message, sentIntent, null);
}
Depending on your application you can do the rest of processing when MY_ACTION_INTENT is sent (indicating the message has actually been sent) or right after sendTextMessage(...) returns.
From API Level 19 there are some interesting features you may found useful http://developer.android.com/reference/android/provider/Telephony.html
Hope it helps.

Android KitKat 4.4 Hangouts cannot handle Sending SMS intent

Code for sending sms that worked perfectly until Android 4.3 (Jelly Bean) and stopped working since 4.4 (KitKat)
I'm just preparing the text message for the user, but they need to choose the number to send to.
The code I have used is:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
sendIntent.putExtra("sms_body", smsText);
activity.startActivity(sendIntent);
Since it stopped working, I have also tried the ACTION_SEND and ACTION_SENDTO Neither worked, I also tried the sendIntent.setType("vnd.android-dir/mms-sms");, but again nothing worked.
I looked at several answers on Stack Overflow answer 1 and answer 2, but both answers aren't dealing with the requirements I have.
What I would like to do:
Send sms with sms app only, not by all apps that serves the send intent
Prepare the text for the user
Let the user choose the phone number to send the message to
For moderators:
It is not a duplicate questions, since the questions, doesn't ask the exact same thing, the need here is to send sms with no phone number, and none of the questions and answers dealt with that.
I attached a code that solve the problem by doing the following:
Check the OS version
In case that older version (prior to KitKat), use the old method
If new API, check the default sms package. if there is any, set it as the package, otherwise, let the user choose the sharing app.
Here is the code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) //At least KitKat
{
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity); //Need to change the build to API 19
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, smsText);
if (defaultSmsPackageName != null)//Can be null in case that there is no default, then the user would be able to choose any app that support this intent.
{
sendIntent.setPackage(defaultSmsPackageName);
}
activity.startActivity(sendIntent);
}
else //For early versions, do what worked for you before.
{
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
sendIntent.putExtra("sms_body", smsText);
activity.startActivity(sendIntent);
}
This one should work on all android versions and all sms apps (including Hangouts).
public static boolean sendSms(Context context, String text, String number) {
return sendSms(context, text, Collections.singletonList(number));
}
public static boolean sendSms(Context context, String text, List<String> numbers) {
String numbersStr = TextUtils.join(",", numbers);
Uri uri = Uri.parse("sms:" + numbersStr);
Intent intent = new Intent();
intent.setData(uri);
intent.putExtra(Intent.EXTRA_TEXT, text);
intent.putExtra("sms_body", text);
intent.putExtra("address", numbersStr);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent.setAction(Intent.ACTION_SENDTO);
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(context);
if(defaultSmsPackageName != null) {
intent.setPackage(defaultSmsPackageName);
}
} else {
intent.setAction(Intent.ACTION_VIEW);
intent.setType("vnd.android-dir/mms-sms");
}
try {
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
return false;
}
return true;
}
Combining the proposed solutions, the following provides presetting the recipient and the text.
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // Android 4.4 and up
{
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity);
intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + Uri.encode(toContact.toString())));
intent.putExtra("sms_body", text);
if (defaultSmsPackageName != null) // Can be null in case that there is no default, then the user would be able to choose any app that supports this intent.
{
intent.setPackage(defaultSmsPackageName);
}
}
else
{
intent = new Intent(Intent.ACTION_VIEW);
intent.setType("vnd.android-dir/mms-sms");
intent.putExtra("address", toContact.toString());
intent.putExtra("sms_body", text);
}
activity.startActivity(intent);
The only problem remaining is that you end up in Hangouts (Nexus 5), and you might have to press "back" multiple times to effectively cancel the SMS.
In Kotlin following code works:
val defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity)
val sendIntent = Intent(Intent.ACTION_SEND)
sendIntent.type = "text/plain"
sendIntent.putExtra("address", "sms:"+contactNumber)
sendIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_msg_body))
Timber.e("defaultSmsPackageName: "+defaultSmsPackageName)
if (defaultSmsPackageName != null){ //Can be null in case that there is no default, then the user would be able to choose any app that support this intent.
sendIntent.setPackage(defaultSmsPackageName)
activity!!.startActivity(sendIntent)
}

Reply To SMS Message: What is the difference between "compose_mode" = true vs false?

Using the code below or similar, what does the flag "compose_mode" do in regard to replying to an SMS message?
Example Code:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:" + sendTo));
intent.putExtra("compose_mode", true); //What does this line do? What if it was "false"?
startActivity(intent);
Never tried it, but discovered that:
// Exit the app once the SMS is sent
intent.putExtra("compose_mode", true);
http://code.google.com/p/zxing/source/browse/trunk/android/src/com/google/zxing/client/android/result/ResultHandler.java?r=635

Categories

Resources