3

I wrote send and receive sms program sucsessfully but I want to that checked receiver phone number in onReceive method. how can i get the sender phone number from a received sms in android? I wrote this code but it dosent worked!!! please check it and help me.

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

    @Override
    public void onReceive(Context context, Intent intent) {
        // ---get the SMS message passed in---

        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;

        if (bundle != null) {

            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];

            for (int i = 0; i < msgs.length; i++) {
                msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

                //for get sms from special number===============================
                String msg_from = msgs[i].getOriginatingAddress();
                Log.v("msg_from >>",msg_from);     
                if(msg_from.equals("08522215"))
                {
                    //===============================
                str += "SMS from " + msgs[i].getOriginatingAddress();
                str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";
            }

            }
            // ---display the new SMS message---
            // Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
            Intent act = new Intent(context, MainActivity.class);
            act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            act.putExtra("message", str);
            context.startActivity(act);
        }

        abortBroadcast();
        }
    }

manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sms"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.SEND_SMS">
    </uses-permission>
    <uses-permission android:name="android.permission.RECEIVE_SMS">
    </uses-permission>

<uses-permission android:name="android.permission.READ_SMS" />
    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
        <activity android:name=".SMS"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".MainActivity"
                  android:label="@string/app_name"/>
       <receiver
            android:name="com.example.sms.SmsReceiver"
            class="com.example.sms.SmsReceiver" >
            <intent-filter android:priority="100" >
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>


    </application>


</manifest>
user1770370
  • 267
  • 2
  • 7
  • 18

3 Answers3

1

In the bundle object itself the sender number is received.

Techie Manoj
  • 432
  • 3
  • 14
1

Write this in the broadcast receiver

Bundle bundle = intent.getExtras(); 

        Object messages[] = (Object[]) bundle.get("pdus");
        SmsMessage smsMessage[] = new SmsMessage[messages.length];      

        for (int n = 0; n < messages.length; n++) 
        {
            smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]); 
        }
        String receivedMessage = smsMessage[0].getMessageBody().toString().toUpperCase();
        String originatingAddress = smsMessage[0].getOriginatingAddress();
        originatingAddress = (originatingAddress!=null && originatingAddress.length()>3)?(originatingAddress.substring(3)):("") ;

The Originating address above is actually the sender number...

EDIT

you probably used

android.telephony.gsm.SmsMessage

that is deprecated. Use this instead:

android.telephony.SmsMessage
drulabs
  • 3,071
  • 28
  • 35
  • This is a working code I am using it in 3 of my apps... Anyway I received you e-mail, will reply after checking... – drulabs Oct 27 '12 at 12:01
  • I made an edit in my answer please check. I am also sending you the SmsReceiver.java file that you sent. I tested it, working just fine. – drulabs Oct 27 '12 at 12:34
  • I test your code but it didnt worked...it for any sms , open program and show content received sms in textview!!!!!!!! – user1770370 Oct 27 '12 at 13:10
  • Are you getting the message in the logs ???? if Yes then change the way you pass, pass it in a bundle and set it as intent.putExtras(bundle); if No check the way you registered the receiver in the manifest. I am so sorry but I am unable to understand... are you getting the message in you activity? You do want sender number right? And I see that you are not even using the originating address (sender number). pass it through intent.putExtras(); ARE YOU GETTING THE MESSAGE AND SENDER NUMBER IN THE LOGS ? – drulabs Oct 27 '12 at 13:11
-1

Try the following code

Don't forget to give SMS receive permission in manifest file

<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>





public class SmsReceiver extends BroadcastReceiver{

private Intent intent;
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

 /* ************** BIG COMMENT STARTS *******
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras(); 

    SmsMessage[] msgs = null;
    SmsMessage[] phonenum =null;//

    String str = ""; 
    String PhoneNUMBER =""; //

    if (bundle != null)
    {

        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");


        msgs = new SmsMessage[pdus.length];            
        for (int i=0; i<msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
            str += "SMS from " + msgs[i].getOriginatingAddress();                     
            str += " :";
            str += msgs[i].getMessageBody().toString();
            str += "\n";        
        }


        //---retrieve the SMS senders number ---
        phonenum = new SmsMessage[pdus.length];
        for (int i = 0; i < phonenum.length; i++) {
            phonenum[i]=SmsMessage.createFromPdu((byte[])pdus[i]);
            PhoneNUMBER += phonenum[i].getOriginatingAddress();
        }


        //---display the new SMS message---
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        Toast.makeText(context, PhoneNUMBER, Toast.LENGTH_LONG).show();







    } 
ProgramFOX
  • 6,131
  • 11
  • 45
  • 51