简体   繁体   English

Android:如何预填充短信

[英]Android: how to pre-populate an SMS message

I am writing an application that needs to pre-populate an SMS message with the first name of the contact when using the stock android SMS messenger . 我正在编写一个应用程序,当使用普通android SMS Messenger时,该应用程序需要使用联系人的名字预填充SMS消息。

For example if my contacts name is Alex Smith. 例如,如果我的联系人姓名为Alex Smith。

When I select Alex Smith to type a message to, I want the text box to already have 当我选择Alex Smith输入消息时,我希望文本框已经有

'Alex, ' 'Alex,'

at the beginning of the SMS. 在SMS的开头。

Please how can I achieve that? 请问我该如何实现?

在此处输入图片说明

Fetch the contact name from contacts, and do like as follows: 从联系人中获取联系人姓名,方法如下:

String contactName = "Alex"; // get it from selected contact
Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( "sms:" + phoneNumber ) ); 
intent.putExtra( "sms_body", contactName+"," ); 
startActivity( intent );

Just look over here: 只是看这里:

Send SMS via intent 通过意图发送短信

You use a Intent to send the SMS and attach some pre defined text via the EXTRA_TEXT attribute. 您可以使用Intent发送SMS并通过EXTRA_TEXT属性附加一些预定义的文本。 Quite easy to do. 很容易做到。 And it works for the normal SHARE intent as well. 它也适用于正常的SHARE意图。

You can't change system apps, but you can send "share intent" from your app to SMS App. 您无法更改系统应用程序,但是可以将“共享意向”从您的应用程序发送到SMS应用程序。 As you remember, when you push "share" button in some apps, you can share smth with SMS. 您还记得,在某些应用程序中按“共享”按钮时,您可以与SMS共享smth。 (You'll have SMS with text that app put in intent). (您将收到带有应用意图的文本的SMS)。 SO, put contact's name in "share intent" and send it to SMS app. 因此,将联系人的姓名放在“共享意图”中并将其发送到SMS应用程序。

it's bit late, but i hope it will help future users. 有点晚了,但我希望它将对将来的用户有所帮助。

void prePopulateSMS(final String mblNumVar, final String smsMsgVar) {
    Log.d(TAG, "prePopulateSMS called. smsMsgVar: " + smsMsgVar);


    Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW);
    smsIntent.setType("vnd.android-dir/mms-sms");
    smsIntent.putExtra("address", mblNumVar);
    smsIntent.putExtra("sms_body", smsMsgVar);
    startActivity(smsIntent);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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