0

I registered a broadcast receiver in my manifest like this :

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <receiver android:name=".SMSReceiver" android:permission="android.permission.RECEIVE_SMS" android:exported="true">
        <intent-filter android:priority="9999">
            <action android:name="android.provider.telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
</application>

And my SMSReceiver class like this :

public class SMSReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if     (intent.getAction().equalsIgnoreCase("android.provider.Telephony.SMS_RECEIVED")) {
...

But the code in the onReceive method is never called. I've tried without the android:exported and android:permission tags in the receiver, but nothing works.

Eko
  • 1,487
  • 1
  • 18
  • 36
  • Did you launch `MainActivity` at least once after installation to bring the app out of the stopped state? Also, remove `android:permission="android.permission.RECEIVE_SMS"` from the `` element. – Mike M. Jan 25 '16 at 18:53
  • Yes I did. Then I kill the app with the android task manager – Eko Jan 25 '16 at 18:56
  • Then that is most likely forcibly stopping your app, and putting it back into the stopped state. Your Receiver won't work again until the you launch the app again. – Mike M. Jan 25 '16 at 19:01
  • Actually, I just noticed, you want to change that permission to `BROADCAST_SMS` instead, since you don't have that one specified. – Mike M. Jan 25 '16 at 19:07
  • Still not working :( – Eko Jan 25 '16 at 19:56
  • 1
    `telephony` needs to be capitalized in the ``'s ``. That is, `"android.provider.Telephony.SMS_RECEIVED"`. Sorry, I would've noticed earlier, but I thought your title meant that it _did_ work when the app was running. – Mike M. Jan 25 '16 at 20:01
  • I have the same problem. When the app is running, SmsReceiver is triggered and working normal. But when the app is not running (the screen is off), SmsReceiver is not triggered. – Plugie Feb 18 '18 at 04:13
  • I have the same problem as @Plugie When the app is running, SmsReceiver is triggered and working normal. But when the app is not running SmsReceiver is not triggered – Joshua Karanja Feb 22 '22 at 23:22

2 Answers2

0

Add RECEIVE_SMS permission along with READ_SMS.

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

This should work.

nadafafif
  • 561
  • 6
  • 10
-1

Since android 4.4 you have to implement some feature to change your sms app to the default sms app and getSms/MMS. And only the default app get sms's/mms's notification.

This answer can help you to do that : link

Community
  • 1
  • 1
Benoît Verhaeghe
  • 612
  • 1
  • 6
  • 21
  • This is not correct. Since 4.4, the `SMS_RECEIVED` action is still broadcast, and it cannot be aborted. Any app registered for that broadcast can receive it. – Mike M. Jan 25 '16 at 19:28