注册 登录
编程论坛 Android开发

关于接受短信的问题

yu1543054075 发布于 2016-03-21 16:42, 4815 次点击
public class SMS_receiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String action = intent.getAction();
        if (action.equals(MainActivity.SMS_RECEIVER)) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                //在Android设备中,接收到的SMS是以pdu协议的编码形式来进行传递的。
                //在短信广播中可以获取pdu数组
                /*
                 * public Object get (String key)
                 * Returns the entry with the given key as an object.
                 */
                //获取pdus原始数据
                Object[] object = (Object[]) bundle.get("pdus");
                //实例化短信类
                SmsMessage[] messages = new SmsMessage[object.length];
                /*
                 * public static SmsMessage createFromPdu (byte[] pdu)
                 * Create an SmsMessage from a raw PDU.
                 */
                for (int i = 0; i < object.length; i++) {
                    //原始数据转换为短信类数据
                    messages[i] = SmsMessage.createFromPdu((byte[]) object[i]);
                }
            
                /*
                 * public String getDisplayOriginatingAddress ()
                 * 返回源地址,或电子邮件地址,如果这个消息是从电子邮件网关。如果起始地址不可用,返回空。
                 * public String getMessageBody ()
                 * 如果它存在,并以文本为基础,则返回该消息体。
                 */
                SmsMessage message = messages[0];    //重组短信信息类
                Toast.makeText(
                        context,
                        "接收到消息的号码是:" + message.getDisplayOriginatingAddress()
                                + "\n接收到的消息是" + message.getMessageBody(), 1000)
                        .show();
                Log.i(MainActivity.TAG, "接收到消息的号码是:"
                        + message.getDisplayOriginatingAddress() + ", 接收到的消息是"
                        + message.getMessageBody());


Object[] object = (Object[]) bundle.get("pdus");?这句代码的意义在哪里,为什么要将Object类型强制转换成Object[],
SmsMessage message = messages[0];    ?这句话是什么意思
请大神指导一下,很着急!!!!
0 回复
1