Example usage for android.telephony SmsMessage createFromPdu

List of usage examples for android.telephony SmsMessage createFromPdu

Introduction

In this page you can find the example usage for android.telephony SmsMessage createFromPdu.

Prototype

public static SmsMessage createFromPdu(byte[] pdu, String format) 

Source Link

Document

Create an SmsMessage from a raw PDU with the specified message format.

Usage

From source file:Main.java

/**
 * Read the PDUs out of an SMS_RECEIVED_ACTION or a
 * DATA_SMS_RECEIVED_ACTION intent.//  w w w .java2 s. c o  m
 *
 * @param intent the intent to read from
 * @return an array of SmsMessages for the PDUs
 */
public static SmsMessage[] getMessagesFromIntent(Intent intent) {
    Object[] messages = (Object[]) intent.getSerializableExtra("pdus");
    String format = intent.getStringExtra("format");

    int pduCount = messages.length;
    SmsMessage[] msgs = new SmsMessage[pduCount];

    for (int i = 0; i < pduCount; i++) {
        byte[] pdu = (byte[]) messages[i];
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            msgs[i] = SmsMessage.createFromPdu(pdu, format);
        } else {
            msgs[i] = SmsMessage.createFromPdu(pdu);
        }
    }
    return msgs;
}

From source file:SMSBroadcastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    if (SMS_RECEIVED.equals(intent.getAction())) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            String format = bundle.getString("format");
            final SmsMessage[] messages = new SmsMessage[pdus.length];
            for (int i = 0; i < pdus.length; i++) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format);
                } else {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                }//from   w w w. j  av  a2 s  . c  o m
                Toast.makeText(context, messages[0].getMessageBody(), Toast.LENGTH_SHORT).show();
            }
        }
    }
}