3

我已经尽可能接近 Google 的示例来实现 GCM,但默认的 GCMBroadcastReceiver 由于缺少 WAKE_LOCK 权限而抛出 SecurityException。不过,我在我的清单中需要它,所以 AFAIK 它应该在运行时拥有该权限。这是我的清单的相关部分:

<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
4

2 回答 2

1

我没有在我注册接收消息的活动的 onDestroy() 方法中调用 GCMRegistrar.onDestroy()。由于某种原因,导致它抱怨没有 WAKE_LOCK,但仅在 4.0.4 设备上。

于 2012-07-09T22:37:15.227 回答
0

你应该使用:

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

代替:

<permission
    android:name="android.permission.WAKE_LOCK"
    android:protectionLevel="signatureOrSystem" />
于 2015-07-17T11:22:00.580 回答