我已经尽可能接近google的示例实现了gcm,但是默认的gcmbroadcastreceiver由于缺少wake_lock权限而抛出了一个securityexception。不过,我需要它在我的清单中,所以它在运行时应该得到这个许可。这是我的清单的相关部分:

<permission
    android:name="PACKAGENAME.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="PACKAGENAME.permission.C2D_MESSAGE" />

<permission
    android:name="android.permission.WAKE_LOCK"
    android:protectionLevel="signatureOrSystem" />

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<application
    android:name=".App"
    android:icon="@drawable/app"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar" >
    <service android:name=".GCMIntentService" />

    <receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="PACKAGENAME" />
        </intent-filter>
    </receiver>

我在日志文件中看到的异常是:
07-09 13:32:58.238: E/AndroidRuntime(2723): java.lang.RuntimeException: Unable to start receiver com.google.android.gcm.GCMBroadcastReceiver: java.lang.SecurityException: Neither user 10072 nor current process has android.permission.WAKE_LOCK.
07-09 13:32:58.238: E/AndroidRuntime(2723):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2126)
07-09 13:32:58.238: E/AndroidRuntime(2723):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
07-09 13:32:58.238: E/AndroidRuntime(2723):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1197)
07-09 13:32:58.238: E/AndroidRuntime(2723):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 13:32:58.238: E/AndroidRuntime(2723):     at android.os.Looper.loop(Looper.java:137)
07-09 13:32:58.238: E/AndroidRuntime(2723):     at android.app.ActivityThread.main(ActivityThread.java:4424)
07-09 13:32:58.238: E/AndroidRuntime(2723):     at java.lang.reflect.Method.invokeNative(Native Method)
07-09 13:32:58.238: E/AndroidRuntime(2723):     at java.lang.reflect.Method.invoke(Method.java:511)
07-09 13:32:58.238: E/AndroidRuntime(2723):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-09 13:32:58.238: E/AndroidRuntime(2723):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-09 13:32:58.238: E/AndroidRuntime(2723):     at dalvik.system.NativeStart.main(Native Method)
07-09 13:32:58.238: E/AndroidRuntime(2723): Caused by: java.lang.SecurityException: Neither user 10072 nor current process has android.permission.WAKE_LOCK.
07-09 13:32:58.238: E/AndroidRuntime(2723):     at android.os.Parcel.readException(Parcel.java:1327)
07-09 13:32:58.238: E/AndroidRuntime(2723):     at android.os.Parcel.readException(Parcel.java:1281)
07-09 13:32:58.238: E/AndroidRuntime(2723):     at android.os.IPowerManager$Stub$Proxy.acquireWakeLock(IPowerManager.java:279)
07-09 13:32:58.238: E/AndroidRuntime(2723):     at android.os.PowerManager$WakeLock.acquireLocked(PowerManager.java:285)
07-09 13:32:58.238: E/AndroidRuntime(2723):     at android.os.PowerManager$WakeLock.acquire(PowerManager.java:264)
07-09 13:32:58.238: E/AndroidRuntime(2723):     at com.google.android.gcm.GCMBaseIntentService.runIntentInService(GCMBaseIntentService.java:235)
07-09 13:32:58.238: E/AndroidRuntime(2723):     at com.google.android.gcm.GCMBroadcastReceiver.onReceive(GCMBroadcastReceiver.java:46)
07-09 13:32:58.238: E/AndroidRuntime(2723):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2119)
07-09 13:32:58.238: E/AndroidRuntime(2723):     ... 10 more

最佳答案

我没有在注册接收消息的活动的ondestroy()方法中调用gcmregistrar.ondestroy()。由于某种原因,它抱怨没有wake_锁,而只在4.0.4设备上。

关于android - GCMBroadcastReceiver缺少 list 中声明的​​WAKE_LOCK权限,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11401264/

10-08 23:21