我是 Google 云消息传递的初学者。我正在开发一个演示应用程序来检查 GCm 的功能。我已经创建了一个 GCM 项目,并且正在检查设备是否已注册。我正在使用以下代码,
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, "483910217912");
Log.d(tag, "Registered");
} else {
Log.v(tag, "Already registered");
}
}
我的清单文件是,
<uses-sdk android:minSdkVersion="8" />
<!-- App 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" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission android:name="my_app_package.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="my_app_package.permission.C2D_MESSAGE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<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="my_app_package" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
<activity
android:name=".GCmExampleActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
我已将 gsm jar 文件包含到我的项目中。
它给出的 Logcat 错误是,
10-11 13:44:59.001: E/AndroidRuntime(339): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kochar.it.GCM/com.kochar.it.GCM.GCmExampleActivity}: java.lang.UnsupportedOperationException: Device does not have package com.google.android.gsf
错误就行了,
GCMRegistrar.checkDevice(this);