android developer tiny share-20160831

本文介绍如何使用Android的Intent来发送短信(SMS)和多媒体短信(MMS),包括设置短信内容、主题及附件等,并提供了示例代码。

今天讲使用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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值