【发布时间】:2013-07-20 03:06:32
【问题描述】:
我正在向“Learning Android”学习 - Oreilly - Marko Gargenta。
我在第 11 章(广播接收器)
我按照这本书做了,一切正常。但是我有一个关于如何使用自定义权限来限制在单个应用程序中发送和接收广播的问题。
这本书对这个主题很清楚。但我觉得少了点什么。
接收方和发送方如何相互告知不同的权限?
在AndroidManifest.xml 文件中:
<permission
android:name="saleh.yamba.SEND_TIMELINE_NOTIFICATIONS"
android:description="@string/send_timeline_notifications_permission_description"
android:label="@string/send_timeline_notifications_permission_label"
android:permissionGroup="android.permission-group.PERSONAL_INFO"
android:protectionLevel="normal" />
<permission
android:name="saleh.yamba.RECEIVE_TIMELINE_NOTIFICATIONS"
android:description="@string/receive_timeline_notifications_permission_description"
android:label="@string/receive_timeline_notifications_permission_label"
android:permissionGroup="android.permission-group.PERSONAL_INFO"
android:protectionLevel="normal" />
<uses-permission android:name="saleh.yamba.SEND_TIMELINE_NOTIFICATIONS" />
<uses-permission android:name="saleh.yamba.RECEIVE_TIMELINE_NOTIFICATIONS" />
在发送广播的服务中:
Intent intent = new Intent("saleh.yamba.NEW_STATUS");
updaterService.sendBroadcast(intent, "saleh.yamba.RECEIVE_TIMELINE_NOTIFICATIONS");
这里,发送者使用saleh.yamba.RECEIVE_TIMELINE_NOTIFICATIONS权限发送意图,好的,接收者如何知道这个权限?
在通过BroadcastReceiver接收广播的Activity中:
TimelineReceiver receiver;
IntentFilter filter;
protected void onCreate(Bundle savedInstanceState)
{
receiver = new TimelineReceiver();
filter = new IntentFilter("saleh.yamba.NEW_STATUS");
}
protected void onResume()
{
this.registerReceiver(receiver, filter, "saleh.yamba.SEND_TIMELINE_NOTIFICATIONS", null);
}
protected void onPause()
{
this.unregisterReceiver(receiver);
}
private class TimelineReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//do something.
}
}
这里接收者通过另一个权限接收它。好的,
接收者如何知道saleh.yamba.RECEIVE_TIMELINE_NOTIFICATIONS.
接收者部分的代码中没有任何内容表明只有当接收者拥有saleh.yamba.RECEIVE_TIMELINE_NOTIFICATIONSpermission 时才会调用BroadcastReceiver。
【问题讨论】:
标签: android permissions