今天讲使用intent实现发送短信。
Text Messaging
Compose an SMS/MMS message with attachment
To initiate an SMS or MMS text message, use one of the intent actions below and specify message details such as the phone number, subject, and message body using the extra keys listed below.
Action
ACTION_SENDTO or
ACTION_SEND or
ACTION_SEND_MULTIPLE
Data URI Scheme
sms:<phone_number>
smsto:<phone_number>
mms:<phone_number>
mmsto:<phone_number>
Each of these schemes are handled the same.
MIME Type
"text/plain"
"image/*"
"video/*"
Extras
"subject"
A string for the message subject (usually for MMS only).
"sms_body"
A string for the text message.
EXTRA_STREAM
A Uri pointing to the image or video to attach. If using the ACTION_SEND_MULTIPLE action, this extra should be an ArrayList of Uris pointing to the images/videos to attach.
Example intent:
public void composeMmsMessage(String message, Uri attachment) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType(HTTP.PLAIN_TEXT_TYPE);
intent.putExtra("sms_body", message);
intent.putExtra(Intent.EXTRA_STREAM, attachment);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}If you want to ensure that your intent is handled only by a text messaging app (and not other email or social apps), then use the ACTION_SENDTO action and include the "smsto:" data scheme. For example:
public void composeMmsMessage(String message, Uri attachment) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setData(Uri.parse("smsto:")); // This ensures only SMS apps respond
intent.putExtra("sms_body", message);
intent.putExtra(Intent.EXTRA_STREAM, attachment);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}Example intent filter:
<activity ...>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<data android:type="text/plain" />
<data android:type="image/*" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>Note: If you're developing an SMS/MMS messaging app, you must implement intent filters for several additional actions in order to be available as the default SMS app on Android 4.4 and higher. For more information, see the documentation at Telephony.
本文介绍如何使用Android的Intent来发送短信(SMS)和多媒体短信(MMS),包括设置短信内容、主题及附件等,并提供了示例代码。
223

被折叠的 条评论
为什么被折叠?



