Any further documentation/tutorial/example of sendBroadcastAsUser? - android

The only documentation on sendBroadcastAsUser says this:
sendBroadcastAsUser(Intent intent, UserHandle user, String
receiverPermission)
Version of sendBroadcast(Intent, String) that allows you to specify
the user the broadcast will be sent to.
But I've not been able to find any examples of how the user is specified, or how you would set up a listener to listen to such a broadcast.
Within the AOSP source I found this example:
Intent intent = new Intent(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
intent.putExtra(PhoneConstants.STATE_KEY,
DefaultPhoneNotifier.convertCallState(state).toString());
if (!TextUtils.isEmpty(incomingNumber)) {
intent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, incomingNumber);
}
intent.putExtra(PhoneConstants.SUBSCRIPTION_KEY, subId);
mContext.sendBroadcastAsUser(intent, UserHandle.ALL,
android.Manifest.permission.READ_PHONE_STATE);
What does UserHandle.ALL mean as the specified user, if it literally means "all" then what is the point in using sendBroadcastAsUser() with ALL rather than just use the vanilla sendBroadcast()?
Would my app be able to listen to this particular system broadcast?

sendBroadcast() - broadcasts the intent only to apps running in the current user
sendBroadcastAsUser allows to send broadcast to apps running in other apps. But this requires INTERACT_ACROSS_USERS_FULL permission which is not available to 3rd party apps. This is the reason, the api is not documented
what is the point in using sendBroadcastAsUser() with ALL rather than
just use the vanilla sendBroadcast()?
sendBroadcastAsUser allows to send broadcast to a particular user , or in case UserHandle.ALL is used, to all the users.
Would my app be able to listen to this particular system broadcast?
Yes, any broadcast sent to the user in which your app is running, will be able to listen to the broadcast as long as your app has permission to recieve that broadcast

Related

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.

Detect when app is opened with a service

I want to be able to detect when an application is opened and notify the user of something at the moment related to that same application but I don't know how to do this.
The user opens my app
I intent the service (background / foreground) and it successfully starts
Whenever the user opens another application I want to "catch it" and present a notification to the user
How can this be made? Are there any event listeners i need to use? Thank you very much.
If you are trying to catch "open app" intents in general, then it depends on how the app defined the intent. If it specified a class (explicit intent) then it will generally not be visible to your app unless the device is rooted, for example.
Implicit intents are broadcast and you simply need to define an intent filter in order to receive them. These are intents that allow Android and/or the user to select the appropriate app target based on data sent with the intent.
There are both useful and malicious motivations to do the kind of thing you are asking about. Read this:
Android Intent Security
And also the posted comment on learning about intents overall.
This is really simple. Here I am trying to figure out the solution. When your app goes on onPause() state then broadcast a message using BroadcastReceiver. On the other hand in another app just register for that broadcast.

send broadcast restricted to dynamic list of applications

my application defines events that other apps installed on the device could be registered to (like other apps can register to google play services location updates and activity recognition events).
google play services process notify this types of events via PendingIntent provided by the receiving app.
I prefer to do it by sending a broadcast restricted by permissions, and I think that from a good reason: PendingIntent cannot be saved in persistent way, so every time my app will shout down, the apps that that registered to my events will have to re-register to my events..
the problem: in the link I provided, you can see how to restrict broadcast by custom permission declared in both sides (the broadcast sender application, and the receiving application), but what if I need to decide from the senders side dynamically from a list I'm getting from the server, what are the apps that allowed to receive the broadcast?
I don't find any reference or example in the web how to do so.
I guess it suppose to be possible somehow, because seems like google play services is using this approach for notifying GCM push notifications only to the relevant receiver...
please help me understand how can I do such a thing.
but what if I need to decide from the senders side dynamically from a list I'm getting from the server, what are the apps that allowed to receive the broadcast? I don't find any reference or example in the web how to do so.
Use an explicit Intent, where you have set the ComponentName of the specific BroadcastReceiver that you are "broadcasting" to. You will need to "broadcast" such an explicit Intent once per app that is "allowed to receive the broadcast".

Is it possible to scope a broadcast to an application?

I want to use sendBroadcast to broadcast an intent that can only be received by that application that originated it.
The use case is for an authentication module I'm planning to use between several projects I've built. Currently when a 401 (authorization failed) response code is received it broadcasts an intent that can then be wired in the manifest to bring the user back to the login page.
So just to be clear I'm wondering if I can do this:
Intent i = new Intent("my.custom.logout.broadcast.path.that.will.be.the.same.in.multiple.apps");
sendBroadcast(i);
And somehow the intent doesn't get picked up by other apps that use exactly the same un-modified code base.
Not part of the question, but you should be using permissions for restricting who can send/receive the Broadcasts, especially since you're dealing with authentication. In fact, I would highly recommend looking into the AccountManager (in API demos)
Moving on.. one solution is to pass the originating application's package name or a particular permission string in an Intent extra, and when you respond, use that package name or permission in the response, the same way. Unfortunately you can't target a specific package in a Broadcast Intent.
Yet another solution, is using PendingIntent.getActivity() from the originating application, stuff that PendingIntent into the new for-broadcast Intent as an extra (Intent#putExtra()) and broadcast it using the permissions model above (so sender and receiver can handle permissions properly). When the broadcast receiver has finished doing its thing, it can use the PendingIntent to start the target activity (including the ability to attach additional extras)
For this you can use the LocalBroadcastManager, which is specifically designed for in-app communication.

Broadcast Receiver for Application Purchase/Installation

I have an application with a broadcast receiver, and I'm trying to figure out how to receive the broadcast of when an app from the Market is purchased and/or installed. Is this possible?
I've tried setting a receiver for android.Intent.ACTION_PACKAGE_ADDED and com.android.vending.billing.PURCHASE_STATE etc.
There isn't a way of receiving a broadcast intent on purchase/install of the application. From what I recall reading about this in the past, is that it's a security concern.
From the API, it even states that a new application won't receive the ACTION_PACKAGE_ADDED broadcast intent. PURCHASE_STATE is also irrelevant since the app isn't installed yet at purchase time.

Categories

Resources