public void registerClient()
{
try
{
// Check that the device supports GCM (should be in a try / catch)
GCMRegistrar.checkDevice(this);
// Check the manifest to be sure this app has all the required
// permissions.
GCMRegistrar.checkManifest(this);
// Get the existing registration id, if it exists.
regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals(""))
{
// register this device for this project
GCMRegistrar.register(this, PROJECT_ID);
regId = GCMRegistrar.getRegistrationId(this);
regId = GCMRegistrar.getRegistrationId(this);总是返回一个空字符串。是否已弃用GCMRegister类?
我从sdk / extras / google / gcm / gcm-client路径添加了gcm jar。
我也尝试添加Google Play服务库并执行代码(删除了gcm.jar),但随后显示GCMRegistrar类未找到异常
我已经检查了所有权限,它们似乎是正确的。
这是我的清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gcmclient"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<!-- receives GCM messages -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM connects to Google services -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.READ_OWNER_DATA" />
<!-- wake the processor if a GCM message is received -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="com.example.gcmclient.permission.C2D_MESSAGE"
android:protectionLevel="signature" >
</permission>
<uses-permission android:name="com.example.gcmclient.permission.C2D_MESSAGE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.gcmclient" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.gcmclient" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" >
</service>
</application>
</manifest>
最佳答案
您的代码看起来还可以,但是使用GCM时,您必须同步设置中的帐户。按照步骤。
转到设置--->添加帐户--->谷歌