Broadcast Receiver not working when the App killed in Oreo Why?
Asked Answered
D

2

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>

Dharana answered 2/7, 2018 at 9:7 Comment(6)
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: medium.com/@berriz_/…Massacre
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.Jollenta
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 meDharana
I try your preferred link but its still not working @Hed Shafran, thanks for commentDharana
@Dharana have you got the solution ?Dwightdwindle
Yes @Debugger, i got it by make broadcast receiver explicit intentDharana
P
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.

Peristalsis answered 1/11, 2018 at 12:20 Comment(2)
@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.Peristalsis
@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." 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.Peristalsis
J
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>
Jacklynjackman answered 2/7, 2018 at 9:10 Comment(3)
its still not working, but thanks for reply @Anthony CannonDharana
Have you tried simplifying it? So remove: enabled, exported, RECEIVE_BOOT_COMPLETED, priority, BOOT_COMPLETED.Jacklynjackman
yes, i tried but getting same result @Anthony CannonDharana

© 2022 - 2024 — McMap. All rights reserved.