send SMS Intent in Android

旧街凉风 提交于 2019-12-20 02:47:37

问题


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!!!

回答1:


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)




回答2:


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



来源:https://stackoverflow.com/questions/5013651/send-sms-intent-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!