简体   繁体   中英

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 .

For example if my contacts name is Alex Smith.

When I select Alex Smith to type a message to, I want the text box to already have

'Alex, '

at the beginning of the 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. Quite easy to do. And it works for the normal SHARE intent as well.

You can't change system apps, but you can send "share intent" from your app to SMS App. As you remember, when you push "share" button in some apps, you can share smth with SMS. (You'll have SMS with text that app put in intent). SO, put contact's name in "share intent" and send it to SMS app.

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

}

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