Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How "deliveryIntent" works in Android SMS framework?

Android documentation for SMSManagers sendTextMessage function

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

deliveryIntent if not NULL this PendingIntent is broadcast when the message is delivered to the recipient. The raw pdu of the status report is in the extended data ("pdu")

I could not understand if deliveryIntent is fired when SMS is delivered to destinationAddress or scAddress and what is the meaning of "raw pdu of the status report is in the extended data ("pdu")" and how to get that report? .

I appreciate your effort.

like image 963
Gaurav Agarwal Avatar asked Mar 22 '12 16:03

Gaurav Agarwal


2 Answers

It is broadcast when message is delivered to destinationAddress.

The PDU may be extracted from the Intent.getExtras().get("pdu") when registered BroadcastReceiver receives the Intent broadcast you define with PendingIntent.getBroadcast(Context, int requestCode, Intent, int flags). For example:

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);               
}

Then you need to parse extracted PDU, SMSLib should be able to do that.

like image 113
a.ch. Avatar answered Oct 21 '22 07:10

a.ch.


Just to build on a.ch's answer, heres how you can extract the delivery report from an intent:

 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;
}

Full credit to the great project at: http://code.google.com/p/android-smspopup/

like image 28
DanielGrech Avatar answered Oct 21 '22 06:10

DanielGrech