DeviceAdminReceiver for Android O - android

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.

Related

Receive message when not running, Explicit broadcast and Implicit broadcast on Xamarin Android

My code below doesn't work on androind 8.0+, but works before 8.0.
Thus, it seems it is treated as an implicit broadcast.
[BroadcastReceiver(Enabled = true, Exported = true)]
[IntentFilter(new[] { "notpackagename.com.IntentReceiver.MySampleBroadcastReceiver" })]
public class MySampleBroadcastReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
String value = intent.GetStringExtra("key");
Console.WriteLine(value);
}
}
var intent = new Intent("notpackagename.com.IntentReceiver.MySampleBroadcastReceiver");
intent.PutExtra("key", "MySampleBroadcastReceiver");
SendBroadcast(intent);
I want to know what is defined as Explicit broadcast or Implicit broadcast?
Because I need the app to receive message when it is not running, like below:
https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/broadcast-receivers
The primary difference between the manifest-registered receiver and
the context-registered receiver is that a context-registered receiver
will only respond to broadcasts while an application is running, while
a manifest-registered receiver can respond to broadcasts even though
the app may not be running.
https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/broadcast-receivers
Android identifies two types of broadcasts:
Explicit broadcast – These types of broadcasts target a specific
application. The most common use of an explicit broadcast is to start
an Activity. An example of an explicit broadcast when an app needs to
dial a phone number; it will dispatch an Intent that targets the Phone
app on Android and pass along the phone number to be dialed. Android
will then route the intent to the Phone app.
Implicit broadcast – These broadcasts are dispatched to all apps on
the device. An example of an implicit broadcast is the
ACTION_POWER_CONNECTED intent. This intent is published each time
Android detects that the battery on the device is charging. Android
will route this intent to all apps that have registered for this
event.
Update
Please correct me if I am wrong:
Based on the description below, MySampleBroadcastReceiver above is manifest-registered receiver, so it should be able to receive message. But it is not even working. I have to change it to dynamic registration.
An intent filter is used to register a broadcast receiver so that
Android can properly route messages. The intent filter can be
specified at runtime (this is sometimes referred to as a
context-registered receiver or as dynamic registration) or it can be
statically defined in the Android Manifest (a manifest-registered
receiver).
https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/broadcast-receivers
What is defined as Explicit broadcast or Implicit broadcast?
Explicit broadcast
The sent Intent is a broadcast that shows the Intent. By specifying the name of the Intent component, it is generally used to know the name of the target component, to call the following method. The intent is clear, specifying which component is to be activated, which is typically implemented inside the same application.
Intent.setComponent()
Intent.setClassName()
Intent.setClass()
new Intent(A.this,B.class)
Implicit broadcast
It is implemented by Intent Filter, which is generally used without explicitly indicating the name of the target component. The Android system will handle this intent by finding the most appropriate component based on the action, category, data (URI and data type) set in the implicit intent. Generally used between different applications
Your code is Explicit broadcast
I need the app to receive message when it is not running
Based on my research, it cannot be achievd in Explicit broadcast, If application is closed, Explicit broadcast will be closed.
If you want to achieve application receive messages from server and it will not closed by the Android system, I suggest you to Start the service in the foreground like this link.
How to create service doing work at period time in Xamarin.Forms?

How can I have one android app send a message to another to start it up in android O?

Before android O, I was able to trigger the functionality of a second sister-app based on the action of a first app. I did this by sending the second app an intent that was defined as a broadcast receiver in the second app. All as well.
But apparently as of api 26, any broadcast receivers defined in the manifest that aren't particular system ones (ie, app specific ones like mine) get ignored. I get errors in the logcat to that effect, I forget the details.
The only way I can think to get this to work is to have the second app start on boot (that broadcast receiver still seems to work) and have it start a service that installs a broadcast receiver programatically, and the service keeps enough of the app alive to receive those messages.
I haven't tried that yet because that seems like overkill and very resource intensive just to receive an intent to do something.
Is there any other mechanism to make this work?
Thanks.
But apparently as of api 26, any broadcast receivers defined in the manifest that aren't particular system ones (ie, app specific ones like mine) get ignored
Not true. You're misunderstanding the restriction.
As of API 26, you can no longer receive implicit broadcasts with a Manifest-declared receiver. However, explicit broadcasts are exempt.
Target the receiver explicitly in your Intent:
Intent intent = new Intent("my_action");
intent.setComponent(new ComponentName("com.sister.packagename", "com.sister.packagename.Receiver");
sendBroadcast(intent);
You'll obviously need to use your own action and the proper package/class name, but that will allow you to keep your Manifest-defined receiver.
Make sure you're checking the action in that receiver's onReceive() method, though. By sending an explicit broadcast, Android ignores your intent filter and sends the intent anyway.

Broadcast Receiver on Android Oreo

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.

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.

What exactly is a broadcast intent?

What exactly is a broadcast intent? How is it different from a regular intent?
What are the differences between explicit and implicit broadcast intents?
What does the Intent Sniffer application mean when it says
"the Intent Sniffer tool performs monitoring of runtime routed broadcasts Intents. It does not see explicit broadcast Intents, but defaults to (mostly) unprivileged broadcasts."?
Broadcast intents are intents used to trigger BroadcastReceivers having an Intentfilter matching the broadcasted intent.
This is triggered with Context.sendBroadcast as opposed to other intents that are often used with Context.startActivity.
A broadcast intent could be received by multiple receivers while other intents will be caught by a specific activity (perhaps at the user's choosing).
I guess Intent Sniffer simply listens to all broadcasts it can get its hands on by registering a few receivers with wide intentfilters.

Categories

Resources