我试图了解GCM的工作原理。为此,我复制/粘贴http://developer.android.com/在第34节中提出的代码;实施GCM客户端"。
从服务器发送消息有效,但当我的客户端应用收到消息时,它会崩溃,告诉我有一个:
E/AndroidRuntime(5722): java.lang.RuntimeException: Unable to instantiate receiver
com.test.testnotification3.GcmBroadcastReceiver: java.lang.ClassNotFoundException: Didn't
find class "com.test.testnotification3.GcmBroadcastReceiver" on path:
/data/app/com.test.testnotification3-2.apk
我在互联网上搜索类似的问题,很多人似乎因为他们的清单中的问题(清单中的错误包名称)而得到此错误。但我检查了我的清单,似乎没问题:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.testnotification3"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.test.testnotification3.permission.C2D_MESSAGE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<receiver
android:name="com.test.testnotification3.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.test.testnotification3" />
</intent-filter>
</receiver>
<service android:name="com.test.testnotification3.GcmIntentService" />
<activity
android:name="com.test.testnotification3.MainActivity"
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>
如果您有任何想法...... 非常感谢!
编辑: 这是我的GcmBroadcastReceiver:
package core;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
当然,由于包名更改了,我更换了#34; com.test.testnotification3&#34;通过&#34;核心&#34;对于接收者。
答案 0 :(得分:1)
您必须将接收器放在另一个包中。如果它在主包上,系统将无法实例化它。