情诗网 >情话短信 > 正文

10.发送短信

来源:情诗网    2021-02-14    分类:情话短信

10.1 问题

应用程序需要向外发送短信。

10.2 解决方案

(API Level 4)
使用SMSManager发送文字或数据短信。SMSManager是一个系统服务,用来处理短信发送并把操作的状态反馈给应用程序。SMSManager提供了SmsManager.sendTextMessage()和SmsManager.sendMultipartTextMessage()方法来发送文字短信,并且提供了SmsManager.sendDataMessage()方法来发送数据短信。这些方法都含有有用于传递发送操作状态和将消息传回请求目标的PendingIntent参数。

10.3 实现机制

下面看一个简单的示例,其发送一条短信并监控其状态(参见以下代码清单)。
发送短信的Activity

public class SmsActivity extends Activity {
    //想要监听的设备地址 (电话号码、短代码等)
    private static final String RECIPIENT_ADDRESS = "<ENTER YOUR NUMBER HERE>";

    /* Custom Action Strings for Result Delivery */
    private static final String ACTION_SENT =
            "com.examples.sms.SENT";
    private static final String ACTION_DELIVERED =
            "com.examples.sms.DELIVERED";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Button sendButton = new Button(this);
        sendButton.setText("Hail the Mothership");
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendSMS("Beam us up!");
            }
        });

        setContentView(sendButton);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //监控操作的状态
        registerReceiver(sent, new IntentFilter(ACTION_SENT));
        registerReceiver(delivered, new IntentFilter(ACTION_DELIVERED));
    }

    @Override
    protected void onPause() {
        super.onPause();
        //确保在位于后台时接收器不会激活
        unregisterReceiver(sent);
        unregisterReceiver(delivered);
    }

    private void sendSMS(String message) {
        //Construct a PendingIntent to fire on SMS sent
        PendingIntent sIntent = PendingIntent.getBroadcast(
            this, 0, new Intent(ACTION_SENT), 0);
        //Construct a PendingIntent to fire on SMS delivery confirmation
        PendingIntent dIntent = PendingIntent.getBroadcast(
            this, 0, new Intent(ACTION_DELIVERED), 0);

         //发送短信
        SmsManager manager = SmsManager.getDefault();
        manager.sendTextMessage(RECIPIENT_ADDRESS, null, message,
                sIntent, dIntent);
    }

    /*
     * BroadcastReceiver that is registered to receive events when
     * an SMS message is sent; with the result code.
     */
    private BroadcastReceiver sent = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                //发送成功
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
            case SmsManager.RESULT_ERROR_NO_SERVICE:
            case SmsManager.RESULT_ERROR_NULL_PDU:
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                //发送失败
                break;
            }
        }
    };

    /*
     * BroadcastReceiver that is registered to receive events when
     * an SMS delivery confirmation is received; with the result code.
     */
    private BroadcastReceiver delivered = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                //传递成功
                break;
            case Activity.RESULT_CANCELED:
                //传递失败
                break;
            }
        }
    };
}

要点:
发送短信需要在清单中声明android.permission.SEND_SMS权限。

在本例中,当用户单击按钮时就会通过SMSManager发送一条短信。由于SMSManager是一项系统服务,必须调用静态方法SMSManager.getDefault()获得它的一个引用。sendTextMessage()方法的参数为目标地址(号码)、服务中心地址以及短信内容。要想让SMSManager使用系统默认的服务中心地址,这个参数就应该设为null。
这里注册了两个BroadcastReceiver,用来接收要发出的回调Intent:一个发送操作的状态,另一个是传递的状态。这些接收器只有在操作处于待发送状态时才会被注册,在处理完Intent后就会立即取消注册。

热门文章