4

I am trying to capture the SMS received on the phone, but when the phone receives an SMS message the method 'onReceive' is not called. This is my code:

I have the BroadcastReceiver is declared in the 'AndroidManifest.xml' inside the tag 'application':

    <receiver android:name=".util.IncomingSmsReceiver"
        android:exported="true">
        <intent-filter android:priority="2147483647">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

This is the IncomingSmsReceiver.java

public class IncomingSmsReceiver extends BroadcastReceiver {

    public static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

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

        Log.i(TAG, "onReceive executed");
        if (intent.getAction().equals(SMS_RECEIVED)) {
           ...
        }
   }
}

I'm doing the tests on an emulator Google Nexus 5 with Android 6. When I send a sms (fake) in the emulator a notification appears as if it was received really well and I can use it in the default application that brings the emulator. In the logcat of Android Studio does not appear that you have run the method onReceive, or the code written inside.I've tried to change the priority, I've tried using android:enabled="true", I've tried using registerReceiver and I have not gotten it to work. Does anyone know if I miss something?

Ander Acosta
  • 1,060
  • 1
  • 12
  • 25

1 Answers1

4

Are you using a default messaging app with "Disable other apps" flag on?

Please see this:

"android.provider.Telephony.SMS_RECEIVED" not working on my device (HTC Wildfire) - how to debug?

Edit:

Since you are using Android 6, you should use the new permissions model. Check this out: http://developer.android.com/training/permissions/requesting.html

Community
  • 1
  • 1
Ankur Aggarwal
  • 2,210
  • 2
  • 34
  • 39
  • I have read that response, but it is not I think that is my case. I use a clean emulator, only have installed my application, everything else comes from the factory. – Ander Acosta Oct 27 '15 at 17:24
  • @AnderAcosta Edited my answer, please check again – Ankur Aggarwal Oct 27 '15 at 17:28
  • Works! Thanks! I am going to edit the title of my question. – Ander Acosta Oct 27 '15 at 18:18
  • @AnderAcosta Glad to be of assistance! Since the problem seems to be solved, you might want to [mark one of the answers as accepted](http://stackoverflow.com/help/someone-answers) by clicking the check mark (✓) icon next to the answer on the left. Doing so will mark the question as answered, and will also award both you and me a few reputation points. – Ankur Aggarwal Oct 28 '15 at 04:08
  • Dude, you saved my life... I have Xiaomi Mi 5 device and just couldn't get my BroadcastReceiver to work when receiving SMS that I send from the same device. After reading your post, I went to my device's main messaging app settings and disabled "Mi Message" option. It turns out that it enables sending messages over Wi-Fi to other Mi devices, so when I sent messages to my self, they travelled over Wi-Fi, and I believe that's what caused my BroadcastReceiver not to work. – Salivan Nov 30 '16 at 20:59