停止播放特定号码的短信

时间:2014-07-15 09:38:24

标签: android sms broadcastreceiver

我想停止在设备的通知栏中显示特定号码的短信。我已经实现了代码但它运行不正常。请帮助

public class SMSReceiver extends BroadcastReceiver {

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

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

    String str = "";

    if (bundle != null) {
        // Retrieve the SMS.

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

            if (msgs[i].getOriginatingAddress().equals("5556")) {

                //Stop Broadcasting message
                this.abortBroadcast();
            }
        .............

以下是清单文件

MANIFEST

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.smsmsg.SendSMS"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name=".receiver.SMSReceiver"
        android:permission="android.permission.BROADCAST_SMS"
        android:enabled="true" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
</application>

</manifest>

1 个答案:

答案 0 :(得分:0)

我猜这行不对

 msgs[i].getOriginatingAddress().equals("5556")

在比较检查之前是什么数字?

试试这个

msgs[i].getOriginatingAddress().equals("15555215556")

修改

如果你想要abortBroadcast(),那么你的接收器应该有高优先级,这样当你的接收器只有Ordered Broadcast

类型时,接收器才会先收听回叫
<receiver
    android:name=".receiver.SMSReceiver"
    android:permission="android.permission.BROADCAST_SMS"
    android:enabled="true" >
    <intent-filter android:priority="9999">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

Imp Note 这不适用于Kitkat,因为可以选择应用程序应该接收消息的设备

Sms Receiving in Kitkat