Reputation: 16524
According to the android documentation:
Alternatively, starting with ICE_CREAM_SANDWICH, you can also safely restrict the broadcast to a single application with Intent.setPackage
Is there any way in Gingerbread (using the compatibility library perhaps) to restrict a sendBroadcat() event such that it only sends it to a specified package?
Upvotes: 4
Views: 3284
Reputation: 91331
My first suggestion would be to use LocalBroadcastManager if at all possible. This allows you to completely ignore any security issues.
If you really do need to send the broadcast from one app to another, it is indeed true that registerReceiver() did not respect the setPackage restriction until ICS so you can not rely on it until then. There is no secret trick to do what you want, it's just that the platform doesn't have the facility for it.
That said... if you are to the point of specifying an explicit package name, why not just go all the way and use Intent.setComponent()?
Also keep in mind that even setPackage() or setComponent() are not automatically completely secure -- you are still making an assumption that you know who is implementing that package name, and it is entirely possible for a different app than what you expect to be installed through side-loading, even if you own the name in the Play Store.
Upvotes: 3
Reputation: 24474
As broadcasts are handled by the system, I cannot imagine that there's any way to code in a workaround without touching the system's code (so the compatibility package won't help).
If you're really interested in keeping a broadcast secure, you can follow the Android docs suggestion for pre-Android 4.0 by using permissions:
To enforce a permission when sending, you supply a non-null permission argument to
sendBroadcast(Intent, String)
orsendOrderedBroadcast(Intent, String, BroadcastReceiver, android.os.Handler, int, String, Bundle)
. Only receivers who have been granted this permission (by requesting it with the tag in their AndroidManifest.xml) will be able to receive the broadcast.
Upvotes: 0
Reputation: 38595
The docs say setPackage
was introduced in API Level 4, but perhaps there was a framework change that makes it function different/better in ICS. What about creating your own Intent Filter that your receivers will recognize? The NotePad example near the bottom of this page shows an example: in the manifest, the NoteEditor example specifies
<action android:name="com.android.notepad.action.EDIT_NOTE" />
which is a custom action defined by the app.
Upvotes: 1