Search code examples
androidsmsbroadcastreceiver

Stop broadcasting of SMS for specific number


I want to stop the SMS for being showed in the notification bar of device for a specific number. I have implemented the code but its not working well. Kindly help

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

Following is the manifest file

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>

Solution

  • I guess this line would be wrong

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

    before comparing check what was the number coming??

    Try this

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

    EDIT

    If you want to abortBroadcast() then your receivers should have High Priority so that your receiver will listen the callback first when and only the type is 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 This will not work from Kitkat because there is option to select the device which app should receive messages

    Sms Receiving in Kitkat