6

How can i receive data of incoming message using broadcast receiver in Oreo, its working perfect in before Oreo version,but i am unable to receive in Oreo, i have trying to short out this by help of developer site but there is not any sample code for this only Oreo limitation is given there

Here is my BroadCast Receiver Class

public class SMSReceiver extends BroadcastReceiver


{
String sender,message;
public void onReceive(Context context, Intent intent) {

    Bundle myBundle = intent.getExtras();
    SmsMessage[] messages = null;
    String strMessage = "";

    if (myBundle != null) {
        Object[] pdus = (Object[]) myBundle.get("pdus");
        messages = new SmsMessage[pdus.length];

        SmsMessage shortMessage=SmsMessage.createFromPdu((byte[]) pdus[0]);
        for (int i = 0; i < messages.length; i++) {
            messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            sender=messages[i].getOriginatingAddress();

            String message =shortMessage.getMessageBody();



        Toast.makeText(context, sender+"\n"+message, Toast.LENGTH_SHORT).show();


        }

    }

}

and here is my Manifest

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name"
    android:roundIcon="@drawable/logo"
    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:enabled="true" android:exported="true">
        <intent-filter
            android:priority="1000"
            >
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>


    </service>

</application>

Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
Basant
  • 741
  • 7
  • 16
  • Why are you using the same receiver for sms and boot complete? The receiver should still work in Oreo, here is the proper way to do it: https://medium.com/@berriz_/service-and-boot-completed-on-android-o-6a389eae50f1 – HedeH Jul 02 '18 at 09:47
  • It may be because you may have un-registering broadcast receiver in onDestroy() method of your activity and not re registering it after opening the app. – Android is everything for me Jul 02 '18 at 10:54
  • i am not registering or un-registering broadcast receiver any where to my app, and it working in pre Oreo version of android but not working in Oreo @Android is everything for me – Basant Jul 03 '18 at 08:04
  • I try your preferred link but its still not working @Hed Shafran, thanks for comment – Basant Jul 03 '18 at 08:08
  • @Basant have you got the solution ? – mob_web_dev Jul 24 '18 at 13:03
  • Yes @Debugger, i got it by make broadcast receiver explicit intent – Basant Aug 04 '18 at 07:26

2 Answers2

2

From Android Oreo onwards, most Broadcast receivers need to be registered on runtime instead of the manifest declaration.

BroadcastReceiver myReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        //Do Something
    }
};

Then register the receiver:

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.THE_REQUIRED_ACTION);
registerReceiver(myReceiver, intentFilter);

and unregister:

unregisterReceiver(myReceiver);

You can register /unregister the receivers at runtime, by adding the code above to onResume()/ onPause() respectively.

If you want the receiver to persist even if the app is in the background you can register/unregister in your application class instead. If you want it to persist after the user exits the app, you need to register the receiver inside a service or job scheduler.

Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
  • @RitobrotoGanguly please help me understand what you mean by "completely wrong" and how your link contradicts what I've written above. It might be too early in the morning for me, but I don't see the problem. – Nikos Hidalgo Jul 19 '21 at 08:49
  • 1
    @RitobrotoGanguly please have a look here and reevaluate your comment: "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." https://developer.android.com/guide/components/broadcast-exceptions Also if you have a running service where the broadcast is registered, any trigger of that will be received by your service/app. – Nikos Hidalgo Jul 21 '21 at 06:48
0

I think it could be because you also need to add it as a permission, like this:

<receiver
    android:name=".SMSReceiver"
    android:enabled="true"
    android:exported="true"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
    <intent-filter android:priority="1000">
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
    </intent-filter>
</receiver>
Anthony Cannon
  • 1,245
  • 9
  • 20