2

我的代码有问题,我刚刚按照 GCM 推送消息的谷歌指南并收到此错误:

06-07 10:51:04.371: E/AndroidRuntime(29686): FATAL EXCEPTION: main
06-07 10:51:04.371: E/AndroidRuntime(29686): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.droidersuin.project/com.droidersuin.pushnotifications.PushMainActivity}: java.lang.IllegalStateException: Application does not define permission com.droidersuin.project.permission.C2D_MESSAGE
06-07 10:51:04.371: E/AndroidRuntime(29686):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1959)
06-07 10:51:04.371: E/AndroidRuntime(29686):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1984)
06-07 10:51:04.371: E/AndroidRuntime(29686):    at android.app.ActivityThread.access$600(ActivityThread.java:126)
06-07 10:51:04.371: E/AndroidRuntime(29686):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1150)
06-07 10:51:04.371: E/AndroidRuntime(29686):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 10:51:04.371: E/AndroidRuntime(29686):    at android.os.Looper.loop(Looper.java:137)
06-07 10:51:04.371: E/AndroidRuntime(29686):    at android.app.ActivityThread.main(ActivityThread.java:4456)
06-07 10:51:04.371: E/AndroidRuntime(29686):    at java.lang.reflect.Method.invokeNative(Native Method)
06-07 10:51:04.371: E/AndroidRuntime(29686):    at java.lang.reflect.Method.invoke(Method.java:511)
06-07 10:51:04.371: E/AndroidRuntime(29686):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
06-07 10:51:04.371: E/AndroidRuntime(29686):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
06-07 10:51:04.371: E/AndroidRuntime(29686):    at dalvik.system.NativeStart.main(Native Method)
06-07 10:51:04.371: E/AndroidRuntime(29686): Caused by: java.lang.IllegalStateException: Application does not define permission com.droidersuin.project.permission.C2D_MESSAGE
06-07 10:51:04.371: E/AndroidRuntime(29686): 

这是我的清单

 <activity
        android:name="com.droidersuin.pushnotifications.RegisterPushActivity"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Black.NoTitleBar"
        android:label="@string/app_name" >

    </activity>

    <activity
       android:name="com.droidersuin.pushnotifications.PushMainActivity"
         android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Black.NoTitleBar"
        android:configChanges="keyboardHidden|orientation" >
    </activity>

    <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="com.droidersuin.pushnotifications" />
        </intent-filter>
    </receiver>

    <service android:name="com.droidersuin.pushnotifications.GCMIntentService" />


</application>

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />  
<permission 
    android:name="com.droidersuin.pushnotifications.permission.C2D_MESSAGE" 
    android:protectionLevel="signature"/>
<uses-permission android:name="com.droidersuin.pushnotifications.permission.C2D_MESSAGE" /> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

这是我的包裹:

我的包裹

任何身体帮助我..?我希望有人可以帮助解决这个问题,当您学习教程时总是令人沮丧。

4

4 回答 4

10

添加权限后应该是这个样子

<permission android:name="your package name.permission.C2D_MESSAGE"    android:protectionLevel="signature" />
 <uses-permission android:name="your package name.permission.C2D_MESSAGE" />
于 2013-10-05T17:24:45.397 回答
1

它期待com.droidersuin.project.permission.C2D_MESSAGE许可,但您定义了com.droidersuin.pushnotifications.permission.C2D_MESSAGE许可。

pakcage您的清单中指定了什么?不包括那部分。C2D_MESSAGE 权限应与该包匹配。

于 2013-06-07T13:57:18.177 回答
1

您是否在另一个包上的另一个类上使用 checkManifet 函数?

您应该将 GCMIntentService 放在同一个包上(假设您将管理 regId)。看看GCM 源码中的 checkManifest 函数

 public static void checkManifest(Context context) {
    PackageManager packageManager = context.getPackageManager();
    String packageName = context.getPackageName();
    String permissionName = packageName + ".permission.C2D_MESSAGE";
    // check permission
    try {
        packageManager.getPermissionInfo(permissionName,
                PackageManager.GET_PERMISSIONS);
    } catch (NameNotFoundException e) {
        throw new IllegalStateException(
                "Application does not define permission " + permissionName);
    }
 //....
 }

将其放在同一个包上并重命名清单权限。

于 2013-06-07T13:57:43.603 回答
0

将定义移动到应用程序之前而不是之后。

于 2013-08-23T13:27:06.563 回答