0

我正在研究 GCM。我的应用程序甚至没有启动并崩溃。跟随错误

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- FOR GCM START1 -->
<permission
    android:name="com.ama.gcmdemo.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.ama.gcmdemo.permission.C2D_MESSAGE" />
<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM connects to Google Services. -->
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- FOR GCM END1 -->
<application
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.ama.gcmdemo.MainActivityActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </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.ama.gcmdemo" />
        </intent-filter>
    </receiver>
    <service android:name=".GCMIntentService" />
    <service android:name=".DialogService" />
</application>

并得到以下错误

java.lang.RuntimeException:无法启动活动 ComponentInfo{com.ama.gcmdemo/com.ama.gcmdemo.MainActivityActivity}:java.lang.IllegalStateException:应用程序未定义权限 com.ama.gcmdemo.permission.C2D_MESSAGE

主要活动直接在 com.ama.gcmdemo 包下。

请提出错误在哪里

4

3 回答 3

0

您需要使用 GCM 包名称而不是您的应用程序包名称来定义您的接收器,如下所示:

     <receiver
    android:name="com.google.android.gcm.GCMBroadcastReceiver">

不是

   <receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
于 2013-10-07T12:41:09.330 回答
0

尝试这个..

只需更改接收器内的以下行,

android:name="com.google.android.gcm.GCMBroadcastReceiver"

至,

android:name="com.ama.gcmdemo.GCMBroadcastReceiver"

并确保你有GCMBroadcastReceiver.java你的包裹里面。Bcs,你应该在你的包里有接收器来接收 GCM 消息。

于 2013-10-07T11:56:09.787 回答
0

在清单文件中授予此权限

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

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

<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

代替 package-name 给出你的包名

于 2013-10-07T11:43:49.107 回答