An app I'm working on allows the user to allow the app to read the contents of a confirmation SMS to input the verification code on its own. For all devices using an OS earlier than Oreo (API 26), the implementation of the BroadcastReceiver works correctly and allows a proper reception of the SMS. By this implementation I mean placing the receiver object in the AndroidManifest.
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
However, starting with Oreo, one must explicitly register BroadcastReceivers to the appropriate context. I have implemented this as follows:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
smsReceiver = new SmsReceiver();
IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
intentFilter.addAction(Telephony.Sms.Intents.DATA_SMS_RECEIVED_ACTION);
this.registerReceiver(smsReceiver, intentFilter);
}
This block of code is executed upon receiving permission for Manifest.permission.READ_SMS. The SmsReceiver class extends BroadcastReceiver and overrides its onReceive() method.
Here, I have several questions:
I have tested this implementation and have set breakpoints on my onReceive() method in my SmsReceiver. When an SMS arrives, the app never enters the onReceive() method. Why can this be?
I instantiated my IntentFilter in the way it is described on the Android Developer website, i.e. with the ConnectivityManager.CONNECTIVITY_ACTION action. I know the SmsReceiver works, because the break point in onReceive() is always hit upon registration of the receiver. However, the action is merely the CONNECTIVITY_ACTION. The SMS_RECEIVED_ACTION is never caught by the receiver. Is it absolutely necessary to instantiate the IntentFilter with this action or can one leave this out?
Is there something else I'm missing that could lead to my receiver not catching the arriving SMS?
SMS_RECEIVED
is on the broadcast whitelist, so your original approach with the<intent-filter>
should still work. – HazeghiSMS_RECEIVED
broadcast, when registered in the manifest. I log a message to LogCat, and the message shows up. – HazeghiminSdkVersion
to4
to eliminate some Lint errors (did I mention that this app is old?). – HazeghiTurned SMS permission off and on after some seconds manually and it worked.
Did any one of you solve this problem? – Clarineclarinet