9

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.

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
Pmsc
  • 318
  • 1
  • 4
  • 12

4 Answers4

7

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.

Sebastian
  • 2,896
  • 23
  • 36
Pmsc
  • 318
  • 1
  • 4
  • 12
6

Try declaring your receiver as the following :

<receiver android:name=".SmsReceiver" android:permission="android.permission.BROADCAST_SMS" android:exported="true">
    <intent-filter android:priority="5822" >
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
 </receiver>

this works just fine for me , I only added a flag to tell that this receiver is exported.

Edit: I forgot to add the priority to the intent filter. use high number for the priority.

Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95
Moh Sakkijha
  • 2,705
  • 1
  • 14
  • 19
  • 1
    @Pmsc please check the edited answer, I use a priority for the receiver to make sure my receiver will be the first receiver that will catch the SMS – Moh Sakkijha Oct 21 '13 at 17:25
  • tried with priority="5822" and with the highest integer possible 2147483647 , and still it doesn't receive any broadcast as long as I have the Handcent SMS app installed and set as default for income SM,because the Handcent app receives the broadcast first and aborts the broadcast so none of the other apps receive the broadcast. found a similar topic: http://stackoverflow.com/questions/8803096/sms-received-onreceive-android-2-3-5-not-triggering – Pmsc Oct 22 '13 at 10:59
4

As @Maxim Toyberman said, you need to ask for permission at runtime (as explained here https://stackoverflow.com/a/35972161/3427883 )

basically you need to make sure you have the permission to Receive the SMS as following

        if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.RECEIVE_SMS}, 1);
Abdu
  • 487
  • 4
  • 12
1

I have tried everything and it worked on many phones but not mine,I tried to ask for permission in rum time (I have nexus 5 version 23). it worked for me.

Maxim Toyberman
  • 1,906
  • 1
  • 20
  • 35
  • Hi, you can take a log, I think that your problem related with phone number read in operation system, for example many phones add space or dash between numbers "+98 999-888-2121" – Programer_saeed Oct 15 '17 at 16:06