1

I have a SMS listener that works well in versions prior to Oreo (API 26) but seems to be ignored in API 26. According to documentation, SMS_RECEIVED_ACTION is exempted from the implicit broadcast limitations (link), so no changes should be needed.

Here is my listener:

public class SmsListener extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        (...)
    }
}

I have this on the manifest:

<receiver android:name=".communication.SmsListener"
    android:permission="android.permission.BROADCAST_SMS">
    <intent-filter android:priority="1000">
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
    </intent-filter>
</receiver>

And also set the permissions

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

Have tried with different sample projects like this as well and the outcome is the same, no SMS is captured in API 26.

Can someone provide me a working sample or help me figure out whether I have to change something or if there is a bug in API 26?

André Dias
  • 5
  • 1
  • 5

2 Answers2

1

Providing permissions in the manifest.xml alone wasn't enough and I had to add code for runtime permission request.

Add this code to your MainActiviy:

ActivityCompat.requestPermissions(this, 
            new String[]{Manifest.permission.RECEIVE_SMS},
            MY_PERMISSIONS_REQUEST_SMS_RECEIVE);

Define this at the top of MainActivity Class:

private int MY_PERMISSIONS_REQUEST_SMS_RECEIVE = 10;

And also add this override:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == MY_PERMISSIONS_REQUEST_SMS_RECEIVE) {
        // YES!!
        Log.i("TAG", "MY_PERMISSIONS_REQUEST_SMS_RECEIVE --> YES");
    }
}

See Android documentation for runtime permissions

See BroadcastReceiver SMS_Received not working on new devices , Broadcast Receivers not working in Android 6.0 Marshmallow

Kanaiya Katarmal
  • 5,974
  • 4
  • 30
  • 56
  • 2
    That is related to the changes introduced in API 23, which I already handle properly (having runtime permissions request). The problem I have is related to the new changes in API 26, regarding broadcast receiver limitations. – André Dias Dec 06 '17 at 17:11
-3

Just figured out what happened! The android:priority="1000" causes the issue. Found this on the documentation:

The value must be an integer, such as "100". Higher numbers have a higher priority. The default value is 0. The value must be greater than -1000 and less than 1000.

Source: link

André Dias
  • 5
  • 1
  • 5
  • 1
    This is not answer. I set android:priority="999". BroadcastReceiver sms receiver is triggered when the app is running in foreground. But it's not triggered when the app is going to background or the phone screen is going to off state. – Plugie Feb 18 '18 at 04:37
  • Did u request the Runtime Permissions? – Siva Apr 02 '18 at 01:13
  • The document has changed, and it doesn't say maximum number anymore. – wonsuc Nov 17 '18 at 13:02