Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set permissions in broadcast sender and receiver in android

How do we specify in broadcast sending application that which application can receive this broadcast, and in receiving application that which particular application has the permission to send broadcast to its broadcast receiver...

I am new to android..I read the documentation etc on internet but couldn't find the syntax to specify these permissions.

like image 876
blackfyre Avatar asked Aug 02 '12 03:08

blackfyre


People also ask

How do I know if my broadcast receiver is registered?

A simple solution to this problem is to call the registerReceiver() in your Custom Application Class. This will ensure that your Broadcast receiver will be called only one in your entire Application lifecycle.

Where do I register broadcast receiver?

This example demonstrates how do I register a BroadcastReceiver programtically in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is broadcast sender in Android?

Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.


2 Answers

To control who is able to receive the broadcast message, you can use the method sendBroadcast:

public abstract void sendBroadcast (Intent intent, String receiverPermission) 

where you precise the name of the required permission. If the receiver does not declare this permission, it will not be able to get the message. For example, the broadcast sender can do:

Intent broadcast = new Intent(this, MyBroadcastReceiver.class); sendBroadcast(broadcast, "andro.jf.mypermission"); 

In the manifest of the broadcast sender, a new permission should be declared:

<!--  Declaring the special permission --> <permission android:name="andro.jf.mypermission"          android:label="my_permission"          android:protectionLevel="dangerous"></permission> 

Then, in the application that is supposed to receive this broadcast, you have to declare this permission and say that you use it. In the manifest you can add:

<!--  I use the permission ! --> <uses-permission android:name="andro.jf.mypermission"/> 

and of course, you have to declare your broadcast receiver:

<receiver android:name="MyBroadcastReceiver" android:exported="true" /> 

You can have a look at this post for a complete example of a custom permission and also the android developer page about this. Be carefull with the order of installation of your apps because the one that defines the permission should be installed first.

like image 182
JFL Avatar answered Oct 05 '22 18:10

JFL


If you want to restrict who only can send intents to your broadcast receiver, do it this way:

The broadcast receiver:

<manifest ...>      <!-- Permission declaration -->     <permission android:name="my.app.PERMISSION" />      <receiver          android:name="my.app.BroadcastReceiver"         android:permission="my.app.PERMISSION"> <!-- Permission enforcement for delivering intents to this receiver -->         <intent-filter>             <action android:name="my.app.Action" />         </intent-filter>     </receiver>      ... </manifest> 

The broadcast sender:

<manifest ...>     <!-- We declare we own the permission to send broadcast to the above receiver -->     <uses-permission android:name="my.app.PERMISSION" />      ... </manifest> 

Sending broadcast from the sender Activity to the receiver:

Intent intent = new Intent(); intent.setAction("my.app.Action"); activity.sendBroadcast(intent); 

If you declare the permission like this:

<permission android:protectionLevel="signature" android:name="my.app.PERMISSION" /> 

Then the sender will be able to use this permission and send broadcasts to receiver only when both the sender and the receiver apps are signed by the same developer certificate.

like image 26
petrsyn Avatar answered Oct 05 '22 18:10

petrsyn