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.
精彩评论