我尽可能地将GCM与Google的示例紧密相关,但默认的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
答案 0 :(得分:1)
我没有在我注册接收消息的活动的onDestroy()方法中调用GCMRegistrar.onDestroy()。由于某种原因导致它抱怨没有WAKE_LOCK,而只是在4.0.4设备上。
答案 1 :(得分:0)
您应该使用:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
而不是:
<permission
android:name="android.permission.WAKE_LOCK"
android:protectionLevel="signatureOrSystem" />