Search code examples
androidbroadcastreceiver

Broadcast Receiver not working for SMS


First of all I already searched for possible solutions, tried everything and it still didn't work. I must be missing something.

I am trying to create an app that receives/reads and writes SMS. the write part is working just fine, my broadcast receiver just doesn't catch broadcast.

AndroidManifest.xml

    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" /> 
    <uses-permission android:name="android.permission.READ_SMS" /> 
...
        <receiver android:name=".SmsReceiver"
            android:permission="android.permission.BROADCAST_SMS">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
        </receiver>

SmsReceiver.java

public class SmsReceiver extends BroadcastReceiver {

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

    Toast.makeText(context, "ON RECEIVE BROADCAST", Toast.LENGTH_LONG).show();
    Log.d("ON ","RECEIVE");
     Bundle bundle = intent.getExtras();
        Object[] messages = (Object[]) bundle.get("pdus");
        SmsMessage[] sms = new SmsMessage[messages.length];
        // Create messages for each incoming PDU
        for (int n = 0; n < messages.length; n++) {
            sms[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
        }
        for (SmsMessage msg : sms) {
            Log.e("RECEIVED MSG",":"+msg.getMessageBody());
            // Verify if the message came from our known sender

        }
}

none of the Logs or toasts are fired.

Tried changing action on manifest to android.intent.action.AIRPLANE_MODE just to test the declaration and the broadcast was received, is just not working for the SMS.

UPDATED

Tried on a different phone and it worked. Must be because I am using Handcent SMS, and some how its blocking the broadcast. Either way I need it to be working on every phone independent of the applications installed.


Solution

  • Found a topic that answers my doubt: Suppress / Block BroadcastReceiver in another app.

    Even with the priority set to the maximum possible (999), if another app has the same priority, in this case the Handcent SMS app, the first application that will receive the broadcast is the one that was first installed by the user.

    In my case was the Handcent SMS and because it aborts the broadcast when receiving it, my app doesn't receive anything.