Enforcing Permissions in Broadcasts URI

suggest change

You can require a permission when sending an Intent to a registered broadcast receiver. sendBroadcast(Intent, String) delivers the broadcast only to receivers that hold that permission.

Pass the permission name to Context.sendBroadcast(Intent, String). The receiving app must declare that permission with <uses-permission>. The app that defines the custom permission with <permission> should be installed first.

The method signature is:

void sendBroadcast (Intent intent, String receiverPermission)
//for example to send a broadcast to Bcastreceiver receiver
Intent broadcast = new Intent(this, Bcastreceiver.class);
sendBroadcast(broadcast, "org.quadcore.mypermission");

Declare the custom permission in a manifest:

<!--  Your special permission -->
<permission android:name="org.quadcore.mypermission" 
   android:label="my_permission" 
   android:protectionLevel="dangerous"></permission>

The receiving app declares that it holds the permission. android:permission on the receiver also requires the sender to hold org.quadcore.mypermission:

<!--  I use the permission ! -->
<uses-permission android:name="org.quadcore.mypermission"/>
<!-- along with the receiver -->
<receiver
    android:name="Bcastreceiver"
    android:exported="true"
    android:permission="org.quadcore.mypermission" />

The sending app needs the same <uses-permission> when the receiver sets android:permission.

Note: Both a receiver and a broadcaster can require a permission, and when this happens, both permission checks must pass for the Intent to be delivered to the associated target. The App that defines the permission should be installed first.

Find the full documentation here on Permissions.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents