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>
4

3 回答 3

1

In the bundle object itself the sender number is received.

于 2012-10-27T08:23:51.420 回答
1

在广播接收器中写入

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)):("") ;

上面的Originating address实际上是发件人号码......

编辑

你可能用过

android.telephony.gsm.SmsMessage

已弃用。改用这个:

android.telephony.SmsMessage
于 2012-10-27T08:30:29.407 回答
-1

试试下面的代码

不要忘记在清单文件中授予短信接收权限

<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();







    } 
于 2013-11-11T17:44:35.597 回答