package gr.mybook.mysms2;

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

public class SmsReceiver extends BroadcastReceiver {
	
	public static final String SMS_EXTRA_NAME = "pdus";

	public SmsReceiver() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onReceive(Context context, Intent intent) {
	    // get the SMS message from intent
	    Bundle extras = intent.getExtras();
	    String str = "";
	    if (extras != null) {
	      // retrieve the SMS message received
	      Object[] smsExtra = (Object[]) extras.get( SMS_EXTRA_NAME);
	      for ( int i = 0; i < smsExtra.length; ++i ) {
	        SmsMessage sms = SmsMessage.createFromPdu( (byte[])smsExtra[i]);
	        String body = sms.getMessageBody().toString();
	        String address = sms.getOriginatingAddress();
	        str += "SMS from " + address + " :\n"; 
	        str += body + "\n";
	      }
	      // Display SMS message
	      //Toast.makeText( context, str, Toast.LENGTH_SHORT ).show();
	      Intent bintent = new Intent();
	      bintent.setAction("SMS_RECEIVED_ACTION");
	      bintent.putExtra("sms", str);
	      context.sendBroadcast(bintent);
	    }
	}
}
