1

I have problems with GCMRegistrar, here is the code:

// Make sure the device has the proper dependencies.
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this); <- this line is the problem (I suppose)
regId = GCMRegistrar.getRegistrationId(this);

When I change that line to a comment the app doesn't crash but the regId is empty, and when I try to use that line it makes my app crash, I don't know why so I hope you can help me. :)

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chattest1"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

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

<permission
    android:name="com.example.chattest1.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.chattest1.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.chattest1.RegisAct"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.chattest1.Main"
        android:label="@string/title_activity_regis" >
    </activity>
</application>

</manifest>
4

1 回答 1

1

checkManifest检查您的清单是否包含 GCM 工作所需的定义。IllegalStateException当缺少某些东西时它会抛出。

在您的情况下,您缺少可以从 GCM 接收消息的广播接收器。

GCMRegistrar.getRegistrationId(this)(返回本地存储的注册 ID)仅当您首先调用时才会返回非空值GCMRegistrar.register。请注意,这GCMRegistrar.register是非阻塞的,因此响应不会立即到达。

最后,正如 CommonsWare 所建议的,GCMRegistrar已弃用,鼓励您改用GoogleClassMessagingclass。

于 2013-07-18T21:37:18.990 回答