this is going to be a newbie question, but I'm curious how Intent's ACTION_SENDTO works. The code is like this:
TextView textView = (TextView) findViewById(R.id.number_to_call);
String smsNumber = String.format("smsto: %s", textView.getText().toString());
EditText smsEditText = (EditText) findViewById(R.id.sms_message);
String sms = smsEditText.getText().toString();
Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.setData(Uri.parse(smsNumber));
smsIntent.putExtra("sms_body", sms);
I understand that:
This is an implicit intent
ACTION_SENDTO is the action of the intent
setData takes in data of the intent
But whatever in putExtra, I guess what it does is putting in "extra" data? Immediate after running this intent, it takes me to a normal Messages app with number as smsNumber, and sms as content. My questions are:
How does the intent "know" smsNumber should be in the phone number section, and sms is in content section?
I also guess the name "sms_body" does affect the role of the data after it (aka sms is the content of message)?
Edit: I found an explanation for this. It should be in Common Intents
You can use "smsto:XXXXXXXXXX" in uri to give the phone number like in this example from the post :
ACTION_SEND used to send sms
Uri uri = Uri.parse("smsto:0800000123");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "The SMS text");
startActivity(it);
Related
I know since KitKat the code to start the SMS app is the following:
String default_sms_package_name = Telephony.Sms.getDefaultSmsPackage(context);
intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, body);
if (default_sms_package_name != null) intent.setPackage(default_sms_package_name);
But is there a way to preselect the SMS receiver like the SMS body?
Yes you can use ACTION_VIEW and set as many numbers as you want with address parameter as below.
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setData(Uri.parse("smsto:"));
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address" , new String("0123456789;3393993300"));
smsIntent.putExtra("sms_body" , "Test SMS to Angilla");
startActivity(smsIntent);
How to send a SMS to many recipients in android? I want to send a SMS to many receivers in my application. I know this code:
Uri smsToUri = Uri.parse("smsto:" + 10086);
Intent intent = new Intent(
android.content.Intent.ACTION_SENDTO, smsToUri);
String message = "hello";
// message = message.replace("%s", StoresMessage.m_storeName);
intent.putExtra("sms_body", message);
startActivity(intent);
This work for single recipient. But How to send the message to many recipients by using "ACTION_SENDTO" intent? That is too say, how to call the third-party application to send a SMS to many recipients in phone?
send sms to multiple numbers:
String strnum="10086;10086;10087;10089";
Uri smsToUri = Uri.parse("smsto:" + strnum);
or for Sending SMS to Multiple numbers using SmsManager see this post
Unable to send sms using SmsManager in Android
This is the piece of code I am using to call the SMS application:
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
intent.putExtra("sms_body", body);
intent.putExtra("compose_mode", true);
launchIntent(intent);
On devices with an os version below Android 3.0, the above code is working fine, the SMS page gets opened and the message to be sent and the numbers get prefilled correctly but in Android 3.0 and above devices this is not working anymore.
In Android 3.0 the SMS intent is called and the number gets filled and not the text where as in Android 4.0 the SMS intent is called and the text gets filled and not the number.
Does anyone know the solution for this problem?
This code will works for all versions of android
String smsBody = Resources.getString("InvitationBody", getBaseContext()) + Local.User.FirstName;
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", smsBody);
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
Following code works perfect
String body = "This is the message i need to send";
String num = "smsto:999416231";
String[] tokens = num.split(":");
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("address",tokens[1]);
sendIntent.putExtra("sms_body", body);
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
The code which i have mentioned in my question is use to pass the number as Uri.parse(uri) and its value is "smsto:9941..."
But in the new code i am splitting the text and number.
I know it's possible to share a text message with ACTION_SEND by specifying Intent.EXTRA_TEXT. Same approach works for images - Intent.EXTRA_STREAM.
But how can I add both text and image to the same intent?
you can send text and image via intent like
If you are sending using Intent ACTION then,
To send only one data either text or stream use,
Intent intent = new Intent(Intent.ACTION_SEND);
To send more then one data at a time use,
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
Normally to send from one specific Activity to any another specific activity,
Send
Bitmap bmp = "Your Bitmap";
String txt = "Text";
Intent intent = new Intent(ActivityName.this,SecondFile.class);
intent.putExtra("Text",txt);
intent.putExtra("Img",bmp);
startActivity(intent);
Receive
Intent intent = this.getIntent();
String txt = intent.getStringExtra("Text");
Bitmap bmp = intent.getParcelableExtra("Img");
You could add as many extras as you want to the same Intent :
intent.putExtra(Intent.EXTRA_STREAM,imgUri);
intent.putExtra(Intent.EXTRA_TEXT,text);
I hope I didn't get your question wrong.
I've searched Google for this, but have only found similar examples--not exactly what I need. I simply need to start messaging (SMS) and email intents from my app with their "to" fields already populated. So I need to send a number with the sms intent and an email address with the email intent. Any help would be appreciated.
For the e-mail part :
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {"foo#bar.com"});
emailIntent.setType("text/plain");
startActivity(Intent.createChooser(emailIntent, "Send a mail ..."));
from Only Email apps to resolve an Intent
String recepientEmail = ""; // either set to destination email or leave empty
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:" + recepientEmail));
startActivity(intent);
will filter out all non-email applications.