0

How do I properly register a BroadcastReceiver for incoming SMS in an Activity? Registering it in the manifest is not working, presumably due to Background Execution Limits introduced in API level 26.

The required permissions are in the manifest:

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

As well as the Receiver entry:

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

The Receiver class:

public class SmsReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) { 
        Toast.makeText(context," test sucessfull",Toast.LENGTH_LONG).show();
    }
}

I have requested permission at runtime, too:

public void getPermission() {
    if (ContextCompat.checkSelfPermission(getApplicationContext(),
                                          Manifest.permission.READ_SMS)
        != PackageManager.PERMISSION_GRANTED) {

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

But even registering the Receiver dynamically in an Activity is not working:

sr = new SmsReceiver(); // Broadcast receiver
IntentFilter iff = new IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION);
registerReceiver(sr, iff);
Mike M.
  • 38,532
  • 8
  • 99
  • 95
Ashwani
  • 1
  • 2
  • Why do you specify API levels 29 and above? Are you having specific issues only in those versions? Please [edit] your question to show us the code you have now, and to explain exactly how it's not working. – Mike M. Apr 05 '20 at 03:38
  • Hi , i had mentiion API 29 as we know As part of the Android 8.0 (API level 26) Background Execution Limits, apps that target the API level 26 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest. – Ashwani Apr 06 '20 at 00:26
  • The `SMS_RECEIVED` broadcast is on [the list of exceptions for that restriction](https://developer.android.com/guide/components/broadcast-exceptions) (very last section). That is, you can still register a Receiver class in the manifest for it. – Mike M. Apr 06 '20 at 00:29
  • My Manifest file : ( i have tried with both -registering receiver in manifest as well as in main activity ) --- – Ashwani Apr 06 '20 at 00:53
  • Hi Mike , fully agree with you , however my code still not work , i have tried both way by registering receiver in manifest , aw well as in main activity .. – Ashwani Apr 06 '20 at 00:55
  • All SMS permissions are dangerous permissions, so you need to request them at runtime, too, on Marshamallow and above, if your `targetSdkVersion` is 23+ (which is very likely, these days). If you don't actually have the `RECEIVE_SMS` permission when a message comes in, it will seem like nothing's happening in your app. If you've not done that, you could manually enable that permission, for testing purposes, on your app's page in the device Settings. – Mike M. Apr 06 '20 at 00:56
  • necessary permission i have already added in manifest and have request same at runtime , i can send SMS from my app , m getting toast message for SMS send also , however i am not getting any toast message for SMS received – Ashwani Apr 06 '20 at 00:56
  • Manifest file : – Ashwani Apr 06 '20 at 00:57
  • in main aactivity : registering broadcast recievr : sr=new SmsReceiver(); // Broadcast receiver IntentFilter iff= new IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION); registerReceiver(sr,iff); – Ashwani Apr 06 '20 at 00:58
  • public class SmsReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context," test sucessfull",Toast.LENGTH_LONG).show(); /* – Ashwani Apr 06 '20 at 00:59
  • i am not able to figure out that where i am making some mistake , can we help me out . – Ashwani Apr 06 '20 at 01:00
  • You must explicitly request each individual SMS permission at runtime, if you've not done that already. Beyond that, certain devices have additional restrictions on third-party apps that disallow SMS receipt, unless enabled by the user. If you're testing on an actual device, is it one of those, possibly? Also, please [edit] your question to add your code and such, rather than adding it in comments. – Mike M. Apr 06 '20 at 01:01
  • Mike , i have request permission at runtime and i am able to send the SMS , public void getPermission(){ if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest .permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECEIVE_SMS}, My_Permission); } }// get permissionn end – Ashwani Apr 06 '20 at 01:02
  • Again, please [edit] your question to add code, XML, or logs. Anyway, your logic there is a little off. You're checking if the `READ_SMS` permission has been granted, but then requesting `RECEIVE_SMS` if it's not. You'll need to check and request each individual SMS permission you want; i.e., `RECEIVE_SMS`, `READ_SMS`, and `SEND_SMS`. – Mike M. Apr 06 '20 at 01:08
  • Thanks mike for your help , i am new to android , i will request each individual permission . i was under the impression that , once we have have one sms permission , that should be good . – Ashwani Apr 06 '20 at 01:16
  • Yeah, it used to be like that, before Oreo, due to a bug in the permissions mechanism. However, that's been fixed in recent versions, and now you must explicitly request each individual permission at runtime. – Mike M. Apr 06 '20 at 01:18
  • 1
    Thank you mike , after your guidance , finally code work ...i was making mistake of not asking all the permission . – Ashwani Apr 06 '20 at 01:31
  • The WhatsApp question should be posted separately. One specific issue per post, please. Anyhoo, glad you got the SMS stuff working. Cheers! – Mike M. Apr 06 '20 at 01:40
  • Can someone post the correct answer? I am following the comments but it would be easier if someone could post the answer. – w3bMak3r Jun 24 '20 at 15:26
  • @w3bMak3r Ultimately, the answer for this particular issue was to correct their permissions check and request. They were checking for `READ_SMS`, but should've been checking for `RECEIVE_SMS`. That's all. Nothing changed (breaking, anyway) with SMS API between 19 and 29, so that stuff would all be the same as it always was. It was the permissions change in Oreo that was the issue here. – Mike M. Jun 25 '20 at 00:45

0 Answers0