Broadcast Receiver on Android Oreo - android

I have a network change receiver class and it extends from broadcast Receiver, but I’m not pretty sure that it’s working in android Oreo, does Oreo support broadcast receiver, and if it doesn’t support, what’s the other way to do it

Oreo supports Broadcast Receivers but with some restrictions on Implicit broadcast that are declared in manifest.
Implicit vs Explicit Broadcast:
According to the documentation, an implicit broadcast is a broadcast that does not target that app specifically. For example, ACTION_PACKAGE_REPLACED is an implicit broadcast, since it is sent to all registered listeners, letting them know that some package on the device was replaced.
However, ACTION_MY_PACKAGE_REPLACED is not an implicit broadcast, since it is sent only to the app whose package was replaced, no matter how many other apps have registered listeners for that broadcast.
So any broadcast receivers that we have defined statically within our application manifest that are listening for implicit broadcasts will no longer receive those broadcasts.
The reason for this change is that implicit broadcasts would previously trigger any component that was listening for them within the manifest— this could have an adverse effect on application and device performance due to large numbers of applications registered to receive specific broadcasts all being triggered at the same time.
But there is a list of exceptions when it comes to implicit broadcasts — this means that there are still some which you can register to receive broadcasts for. They are all listed below:
So if the broadcast that you have registered receivers for is on this list, then it will still function as it did previously. However, if the broadcast that you have registered to receive is not on this list then you should use some alternative solution like:
Create the receiver at runtime by calling Context.registerReceiver(),
instead of declaring the receiver in the manifest.
Use a scheduled job to check for the condition that would have
triggered the implicit broadcast.
For more information

It's not supported in Oreo as manifest tag, you must have to register it at an Service/ Activity with context.registerReceiver(). Or you use the WorkManager to schedule something for specific network conditions.

Related

Android Implicit Broadcast Exceptions

Android developer portal states broadcasts which can be registered in manifest itself, but it gives a caution as under
Even though these implicit broadcasts still work in the background,
you should avoid registering listeners for them.
One such broadcast which is exempted is as under:
ACTION_LOCKED_BOOT_COMPLETED, ACTION_BOOT_COMPLETED
Exempted because
these broadcasts are only sent only once, at first boot, and many apps
need to receive this broadcast to schedule jobs, alarms, and so forth.
If we should avoid registering them (as per the above caution), then what is the right approach for ACTION_BOOT_COMPLETED ?
Use case: Sync data with the server and show a notification to user (if any)
If you need your app started at boot time, the only way to do this is to have a manifest-registered BroadcastReceiver.

DeviceAdminReceiver for Android O

Helo,
As is described Broadcast Limitations Android O comes with some limitations on implicit broadcasts, by definition broadcasts received by DeviceAdminReceiver are implicit also it are not in Implicit Broadcast Exceptions, although however, MyDeviceAdminReceiver continue receives broadcasts without dynamic registration.
Should I consider that it are explicit broadcasts ?, or it should be dynamically registered ?
Thanks in advance.
Should I consider that it is explicit broadcasts ?
Yes. Just because an Intent has an action string does not make it implicit. What makes it implicit is not having a ComponentName or package to constrain where the Intent gets broadcast.
In this case, since not everything can listen to those broadcasts, the system is filtering the list of registrants to those eligible to receive them, and is using explicit Intents to only send the broadcast to the eligible receivers.

Until when does a BroadcastReceiver receive intents?

I am a bit confused about this part of documentations:
A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent).
Once your code returns from this function, the system considers the object to be finished and no longer active.
What does "no longer active" mean? It means our receiver won't receive any events with the specified type? Or it means the receiver is destroyed and a new one is created when the event occurs?
Is this any difference about this, between registering receiver in manifest file and in code dynamically?
"Is this any difference about this, between registering receiver in manifest file and in code dynamically?"
Yes, there is a difference. Receivers registered in the manifest file will receive all intent-filters it matches, anytime the OS sends them out. This differs from registering a broadcast receiver in code by the broadcast receiver in code will only listen to broadcast intents while you have set it to listen. The reason you would use this method sometimes, as opposed to just registering it in the manifest, is if you wanted to implement ordered broadcast. For example, (you can build your application in such a way that) a broadcast receiver in an activity would have higher priority than the manifest, that way, if you receive an intent that your application handles, you can present a message in the activity because you know that the user much currently be in your app. If the broadcast receiver is not listening in the activity, then you assume the user is not currently using your app so then you may just want to send a notification. I should mention that ordered broadcast have the ability to abort the propagation of a broadcast intent to the next receiver, which is what you would do if you caught the intent in your activity class ,therefore, the manifest file will only get the intent if the receiver in the activity class did not catch it.
The words "no longer active" mean that the broadcast receiver will just stop doing any work for that particular broadcast. It will still listen to any succeeding broadcast intents just fine.
It means that upon returning from onReceive() the system will consider its process to be empty and aggressively kill it so that resources are available for other more important processes.
You will often use a Service in conjunction with a BroadcastReceiver to keep the containing process active for the entire time of your operation.

android: custom intent is triggered first

I am writing an app to receive the sms and display it on my screen. I declared a broadcast receiver and registered in my activity (in oncreate()). I haven't set any priority in my manifest file as well.
However during run time, my receiver is receiving the sms first then the systems message service is invoked first. Is this the behavior of custom broadcast receivers?
Thx!
Rahul.
Android sends ordered broadcasts for SMS messages. From the reference guide (emphasis mine)
Ordered broadcasts (sent with Context.sendOrderedBroadcast) are
delivered to one receiver at a time. As each receiver executes in
turn, it can propagate a result to the next receiver, or it can
completely abort the broadcast so that it won't be passed to other
receivers. The order receivers run in can be controlled with the
android:priority attribute of the matching intent-filter; receivers
with the same priority will be run in an arbitrary order.
I suspect that due to the lack of priority, the order is being chosen arbitrarily.

ConnectivityManager.CONNECTIVITY_ACTION, always broadcast when registering a receiver?

I'm registering a receiver to capture the ConnectivityManager.CONNECTIVITY_ACTION in code i.e. I am not registering it in the application manifest. All is working fine but I notice that I automatically receive a broadcast as soon as I register my receiver and despite the network being already on.
My question is can I rely on this automatic broadcast to check whether the network is available or not i.e. not explicitly call methods to check the network?
I was only expecting a broadcast when a network changed and not upon registering my receiver, however the fact that I do receive a broadcast is very useful but can I rely on it. Is this a documented action?
Yes, that's called sticky broadcasts. Sticky broadcasts are sent to receiver as soon as registerBroadcast is called.

Categories

Resources