代码之家  ›  专栏  ›  技术社区  ›  Michele

检查广播接收器权限程序

  •  0
  • Michele  · 技术社区  · 12 年前

    我有一个用于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。意向过滤器没有收到调用,因为我强制执行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。

    1 回复  |  直到 12 年前
        1
  •  0
  •   user1801374    10 年前

    我知道这个问题很老了,但我遇到了同样的问题,这个问题出现在谷歌搜索中,所以我们开始吧。

    简单的答案是你不能。

    提到的 methods 用于执行IPC时,例如 bound service 使用AIDL。

    广播接收器未执行IPC,因此您无法从广播接收器获取发件人权限。

    要强制执行该权限,您必须在清单中声明广播接收器的权限。这之所以有效,是因为Android在将其传递给接收者之前强制执行所需的权限。

    权限对于接收器中的所有操作都是通用的。为了处理这个问题,您可以将广播接收器拆分为单独的类,正如@CommonsWare所建议的那样。