0

我在android中编写了发送和接收短信...我在onReceive方法中检查了接收到的带有特殊号码的电话号码(一个接收短信的电话号码),但是这个程序为每个发送短信的电话号码打开!!!!但我不想要它!!!!!!!我的问题是,从打开并自动运行的每个电话号码收到的每个短信的广播接收器类?

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

清单.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

1 回答 1

0

我认为这是因为您使用了以下代码:

if(msg_from.equals("08522215"))

您应该完全设置您的号码(0098913...),或者您可以使用此代码

if(msg_from.contain("08522215")
于 2013-04-28T08:18:04.930 回答