如何在android中的广播发送者和接收者中设置权限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11770794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set permissions in broadcast sender and receiver in android
提问by blackfyre
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.
我是 android 新手。我在互联网上阅读了文档等,但找不到指定这些权限的语法。
采纳答案by Vishwanath.M
use an intent filter in receiver tag in manifest
在清单中的接收者标签中使用意图过滤器
<receiver
android:name="Your receiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="action"/>
<category android:name="category" />
</intent-filter>
</receiver>
To send broadcast to app
向应用程序发送广播
Intent intent = new Intent();
intent.setAction("use same action in receiver");
intent.addcategory("use same category in receiver");
context.sendBroadcast(intent);
回答by JFL
To control who is able to receive the broadcast message, you can use the method sendBroadcast:
要控制谁能够接收广播消息,您可以使用方法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 postfor a complete example of a custom permission and also the android developer pageabout this. Be carefull with the order of installation of your apps because the one that defines the permission should be installed first.
您可以查看此帖子以获取自定义权限的完整示例以及有关此内容的android 开发人员页面。请注意应用程序的安装顺序,因为应首先安装定义权限的应用程序。
回答by petrsyn
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 senderActivity to the receiver:
从发送方Activity 向接收方发送广播:
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 senderwill be able to use this permission and send broadcasts to receiver only when both the senderand the receiverapps are signed by the same developer certificate.
然后发送者将能够使用此权限和发送广播到接收器只有当两个发送者和接收者应用是由同一个开发者证书签名。
回答by Cody
Declare permission
声明权限
First you need declare your permission in your AndroidManifest.xml
首先,您需要在您的 AndroidManifest.xml
<permission android:name="YOUR_PERMISSION_STRING" android:protectionLevel="signature"/>
<uses-permission android:name="com.codylab.photogallery.PRIVATE"/>
the android:name
value is used as permission value and will used later.
the android:name
value 用作权限值,稍后将使用。
Usage
用法
There are two kinds of permission usages related to broadcast receiver:
有两种与广播接收器相关的权限用法:
(1) Control which application can receive your broadcast:
(1) 控制哪个应用程序可以接收您的广播:
String PERMISSION_STRING_PRIVATE_RECEIVER = "YOU_NEED_THIS_TO_RECEIVE_THIS_BROADCAST"
sendBroadcast(intent, PERMISSION_STRING_PRIVATE_RECEIVER);
With this usage, you can control only authorized application can handle the broadcast you sent.
通过这种用法,您可以控制只有授权的应用程序才能处理您发送的广播。
(2) Only handle the broadcasts have the specified permission
(2) 只处理具有指定权限的广播
String PERMISSION_STRING_PRIVATE_BROADCASTER = "ONLY HANDLE BROADCASTS WITH THIS PERMISSION"
IntentFilter filter = new IntentFilter(ACTION_SAMPLE);
registerReceiver(mReceiver, filter, PERMISSION_STRING_PRIVATE_BROADCASTER, null);
With this usage, you can make sure that the broadcaster is authorized.
通过这种用法,您可以确保广播公司获得授权。
回答by Swifty
After half day search and test, based on @JFL's answer, I find the sender app must add both <permission>
tag and <uses-permission>
tag, then the receiver can receive the broadcast with permission. Otherwise, the receiver app won't receive the broadcast.
经过半天的搜索和测试,根据@JFL的回答,我发现发送方应用程序必须同时添加<permission>
标签和<uses-permission>
标签,然后接收方可以在允许的情况下接收广播。否则,接收器应用程序将不会收到广播。