9

SMSManagers sendTextMessage函数的 Android 文档

public void sendTextMessage (String destinationAddress, String scAddress, String text,         
PendingIntent sentIntent, PendingIntent deliveryIntent)

如果不为 NULL,则在将消息传递 给收件人时广播此 PendingIntent。状态报告的原始 pdu 在扩展数据(“pdu”)中

我不明白在将 SMS 传递到destinationAddress 或scAddress 时是否触发了deliveryIntent 以及“状态报告的原始pdu 在扩展数据(“pdu”)中的含义是什么以及如何获取该报告?.

我感谢你的努力。

4

2 回答 2

4

它在消息传递到时广播destinationAddress

PDU可以从Intent.getExtras().get("pdu")注册时BroadcastReceiver接收到您定义的 Intent 广播中提取PendingIntent.getBroadcast(Context, int requestCode, Intent, int flags)。例如:

private void sendSMS(String phoneNumber, String message) {      
    String DELIVERED = "DELIVERED";

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
        new Intent(DELIVERED), 0);

    registerReceiver(
        new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                Object pdu = arg1.getExtras().get("pdu");
                ... //  Do something with pdu
            }

        },
        new IntentFilter(DELIVERED));        

    SmsManager smsMngr = SmsManager.getDefault();
    smsMngr.sendTextMessage(phoneNumber, null, message, null, deliveredPI);               
}

然后你需要解析提取的PDU,SMSLib应该可以做到。

于 2012-05-07T08:38:45.100 回答
2

只是以 a.ch 的回答为基础,以下是如何从意图中提取交付报告:

 public static final SmsMessage[] getMessagesFromIntent(Intent intent) {
    Object[] messages = (Object[]) intent.getSerializableExtra("pdus");
    if (messages == null || messages.length == 0) {
        return null;
    }

    byte[][] pduObjs = new byte[messages.length][];

    for (int i = 0, len = messages.length; i < len; i++) {
        pduObjs[i] = (byte[]) messages[i];
    }

    byte[][] pdus = new byte[pduObjs.length][];
    SmsMessage[] msgs = new SmsMessage[pdus.length];
    for (int i = 0, count = pdus.length; i < count; i++) {
        pdus[i] = pduObjs[i];
        msgs[i] = SmsMessage.createFromPdu(pdus[i]);
    }

    return msgs;
}

完全归功于这个伟大的项目:http ://code.google.com/p/android-smspopup/

于 2012-05-09T22:27:25.140 回答