How to receive sms from a special phone number?

Posted by Pariya on Stack Overflow See other posts from Stack Overflow or by Pariya
Published on 2012-10-28T08:41:45Z Indexed on 2012/10/29 5:03 UTC
Read the original article Hit count: 211

I wrote a send and receive sms in android successfully.

I want my program to be able to receive sms from a special number("+9856874236"). But, if the SMS is from any other number, it should go to the phone's message inbox and not to my application.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

    public class SmsReceiver extends BroadcastReceiver {
        public String str = "";

        @Override
        public void onReceive(Context context, Intent intent) {

            Bundle bundle = intent.getExtras();
            Object messages[] = (Object[]) bundle.get("pdus");
            SmsMessage[] msgs = null;

            if (bundle != null)
            {

                Object[] pdus = (Object[]) bundle.get("pdus");
                SmsMessage smsMessage[] = new SmsMessage[messages.length];      
                String msg_from="";
                for (int n = 0; n < messages.length; n++) 
                {
                    smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]); 
                      msg_from += msgs[n].getOriginatingAddress();
                }

                String receivedMessage = smsMessage[0].getMessageBody().toString().toUpperCase();


                if(msg_from .equals("+989124236870"))  
                {
                for (int n = 0; n < messages.length; n++) 
                {

                    smsMessage[n] = SmsMessage.createFromPdu((byte[]) pdus[n]); 
                    str += "SMS from " + msgs[n].getOriginatingAddress();
                    str += " :";
                    //str += "sms az shomare makhsus";
                    str += msgs[n].getMessageBody().toString();

                    str += "\n";

               abortBroadcast();

               }


                Intent act = new Intent(context, MainActivity.class);
                act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                act.putExtra("message", str);
                context.startActivity(act);
            }


            }

        }
    }

© Stack Overflow or respective owner

Related posts about android

Related posts about android-intent