为什么程序(发送和接收短信)自动打开为Android中每个收到的短信?

时间:2012-10-27 09:41:04

标签: android android-intent sms

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

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

1 个答案:

答案 0 :(得分:0)

我认为这是因为您使用此代码:

if(msg_from.equals("08522215"))

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

if(msg_from.contain("08522215")