Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

broadcastreceiver - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...