【发布时间】:2012-08-11 04:49:21
【问题描述】:
我有一个广播接收器用于我的 C2DM(旧)消息传递,例如
<receiver android:name=".C2DMRegistrationReceiver">
<!-- Receive the actual message -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.test" />
</intent-filter>
<!-- Receive the registration id -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.test" />
</intent-filter>
<intent-filter>
<action android:name="REGISTRY_RETRY" />
<category android:name="com.test" />
</intent-filter>
</receiver>
出于安全原因,您应该为此接收者声明一个权限,例如
<receiver android:name=".C2DMRegistrationReceiver" permission="com.google.android.c2dm.permission.SEND">
我的问题是我的 3. Intent Filter 没有收到呼叫,因为我强制执行 com.google.android.c2dm.permission.SEND 权限。
所以我的问题:有没有办法为一个广播接收器定义 2 个权限,或者我可以在我的 onReceive 代码中为调用者强制执行权限?
我试过了
private boolean checkC2DMPermission(Context context) {
String permission = "com.google.android.c2dm.permission.SEND";
context.enforceCallingPermission(permission, "Keine C2DM Permission");
return true;
}
我还测试了 context.checkCallingPermission(permission) 它的 -1 用于 C2DM 注册意图。 Enforce 给了我一个 SecurityException。
【问题讨论】:
-
为什么不简单地将您用于
REGISTRY_RETRY的逻辑移动到单独的BroadcastReceiver中?如果您希望在此代码和 C2DM 代码之间拥有公共代码,请使用继承或组合。 -
是的,这肯定是最简单的解决方案。我只是好奇,对这个问题有点固执。
标签: android android-intent broadcastreceiver android-permissions