3

我儘可能地將Google GCM的實例與GCMroadcastReceiver一樣實現,但默認GCMBroadcastReceiver由於缺少WAKE_LOCK權限而拋出SecurityException。不過,我需要它在我的清單中,所以AFAIK應該在運行時擁有該權限。這裏是我的清單的相關部分:GCMBroadcastReceiver缺少在清單中聲明的​​WAKE_LOCK權限

<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

注:我已經試過這一個4.0.4設備和2.2設備上,我沒有得到的例外2.2設備。 – jph 2012-07-09 20:53:25

回答

1

我並沒有要求在的onDestroy GCMRegistrar.onDestroy()()的方法我註冊接收消息的活動。出於某種原因,導致它抱怨沒有WAKE_LOCK,但只在4.0.4設備上。

0

你應該使用:

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

代替:

<permission 
    android:name="android.permission.WAKE_LOCK" 
    android:protectionLevel="signatureOrSystem" /> 
相關問題