package com.samples.telephony.receiversms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver {

    private static final String SMS_REC_ACTION = "android.provider.Telephony.SMS_RECEIVED";

    @Override
    public void onReceive(final Context context, final Intent intent) {
        if (intent.getAction().equals(SmsReceiver.SMS_REC_ACTION)) {

            StringBuilder sb = new StringBuilder();

            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                for (Object pdu : pdus) {
                    SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
                    sb.append("\nAddress:" + 
                    		smsMessage.getOriginatingAddress());
                    sb.append("\nDisplay Originating:" + 
                    		smsMessage.getDisplayOriginatingAddress());
                    sb.append("\nDisplay MessageBody: " + 
                    		smsMessage.getDisplayMessageBody());
                    sb.append("\nMessage Body: " + 
                    		smsMessage.getMessageBody());
                    sb.append("\nMessage Class: " + 
                    		smsMessage.getMessageClass());
                    sb.append("\nProtocol Identifier: " + 
                    		smsMessage.getProtocolIdentifier());
                    sb.append("\nPseudo Subject: " + 
                    		smsMessage.getPseudoSubject());
                    sb.append("\nService Center Address: " + 
                    		smsMessage.getServiceCenterAddress());
                    sb.append("\nStatus: " + 
                    		smsMessage.getStatus());
                    sb.append("\nStatus On ICC: " + 
                    		smsMessage.getStatusOnIcc());
                }
            }
            Toast.makeText(context, "SMS Received message" + sb.toString(), Toast.LENGTH_LONG).show();
        }
    }
}
