Search code examples
androidsmsbroadcastreceivermessaging

SMS cannot be prevented using abortBroadcast()?


I am working on a sms blocker application, in which i am using broadcast receiver and abortBroadcast() method - as many ppl have suggested here - to prevent messages from reaching inbox and alerting the user. But in my case, when I send a sms using the emulator, the SMS message won't get blocked, and reaches the inbox, also I get an error :

06-29 09:19:05.854: E/BroadcastReceiver(868): BroadcastReceiver trying to return result during a non-ordered broadcast

which doesn't terminate the app in the emulator, however the application gets terminated when I test it on my phone.

And yes, I have set the receiver's priority to a high number and asked for the permissions as you see here:

<receiver android:name="SMSMonitor">  
    <intent-filter android:priority="9999999">
        <action android:name="android.provider.Telephony.SMS_RECEIVED"></action> 
    </intent-filter>  
</receiver>

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

Finally, here's my code:

public class SMSMonitor extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        boolean isOn = loadState(context,"isOn");// is blocking enabled?
        Bundle bundle = intent.getExtras();
        Object messages[] = (Object[]) bundle.get("pdus");
        SmsMessage smsMessage[] = new SmsMessage[messages.length];
        String mAddress;
        String mBody;
        String mTime;
        if(isOn){
            // if spam blocking is enabled.
            for (int n = 0; n < messages.length; n++) {
                smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
                mAddress=smsMessage[n].getDisplayOriginatingAddress().toString();

                mBody=smsMessage[n].getDisplayMessageBody().toString();

                mTime=getTime();
                if(isBlackList( mAddress)== true) {
                    this.addLog(mAddress, mBody, mTime);
                    abortBroadcast();
                    Toast.makeText(context,"Incoming SMS was blocked and logged.", Toast.LENGTH_LONG).show();  
                }
            }
        }
    }
}

Some one suggested here that the SMS broadcasts can't be aborted because Android won't allow it. But I have seen many guys here suggested using abortBroadcast() to block a sms, and also I know some SMS blocker apps on the market that actually CAN block SMSs. I don't know if they are using abortbroadcast or not.

Any ideas?


Solution

  • "As of Android 1.6, incoming SMS message broadcasts (android.provider.Telephony.SMS_RECEIVED) are delivered as an "ordered broadcast" — meaning that you can tell the system which components should receive the broadcast first." and I am using Android 1.5 And the broadcast is non-ordered!

    Thanks to this guy here link