简体   繁体   中英

send SMS Intent in Android

String x="Hello World";
String y="You Rock!!!";
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", x); 
sendIntent.putExtra("sms_body", y); 
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);

I'm trying to send a multiple message bodies via SMS, but only "You Rock!!!" is displayed. What i want to do is be able to display multiple messages and have it pre-formatted (on different lines).

So for example...

Hello World
You Rock!!!

If you want to send a multi-line message just put a newline between the 2 strings.

x + "\n" + y

if want to send multiple messages there is no way to do that, that I am aware of. You could use [startActivityForResult][1] then in your activities [onActivityResult][2] method you can send then next message.

[1]: http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent , int)

[2]: http://developer.android.com/reference/android/app/Activity.html#onActivityResult(int , int, android.content.Intent)

The problem is that you are overriding the extended data, as putExtra is not adding to a list everything you write inside the Bundle argument (the second one), but resetting its content. That is why you can only see the last part, because you first set the extra named "sms_body" to "Hello World" and then you reset it to "You Rock!!!".

I haven't tried it, but it could work if you do something like this:

String smsBody="Hello World\nYou Rock!!!";
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", smsBody); 
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);

The character \\n is a newline (http://en.wikipedia.org/wiki/Newline) special character, which means that you are writing that string in two lines (splitting them right where \\n is). \\n is present in almost every String representation, so it could work here too. Give it a try and tell us.

By the way and just as an advice, try to give understandable names to variables (not just x or y). If you want to reuse code or you find errors, you may want to know what exactly x or y are.

Best regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM