我最近开始学习Android GCM。我正在尝试Android Developer中here给出的GCM客户端示例,当我尝试单击MainActivity中的发送按钮时,它给了我一些例外,如下所示:
09-06 00:16:41.480: E/AndroidRuntime(1246): FATAL EXCEPTION: main
09-06 00:16:41.480: E/AndroidRuntime(1246): java.lang.RuntimeException: Unable to instantiate receiver com.google.android.gcm.demo.app.GcmBroadcastReceiver: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gcm.demo.app.GcmBroadcastReceiver" on path: DexPathList[[zip file "/data/app/com.google.android.gcm.demo.app-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.google.android.gcm.demo.app-1, /vendor/lib, /system/lib]]
09-06 00:16:41.480: E/AndroidRuntime(1246): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2405)
09-06 00:16:41.480: E/AndroidRuntime(1246): at android.app.ActivityThread.access$1500(ActivityThread.java:141)
09-06 00:16:41.480: E/AndroidRuntime(1246): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
09-06 00:16:41.480: E/AndroidRuntime(1246): at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 00:16:41.480: E/AndroidRuntime(1246): at android.os.Looper.loop(Looper.java:137)
09-06 00:16:41.480: E/AndroidRuntime(1246): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-06 00:16:41.480: E/AndroidRuntime(1246): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 00:16:41.480: E/AndroidRuntime(1246): at java.lang.reflect.Method.invoke(Method.java:525)
09-06 00:16:41.480: E/AndroidRuntime(1246): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-06 00:16:41.480: E/AndroidRuntime(1246): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-06 00:16:41.480: E/AndroidRuntime(1246): at dalvik.system.NativeStart.main(Native Method)
09-06 00:16:41.480: E/AndroidRuntime(1246): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gcm.demo.app.GcmBroadcastReceiver" on path: DexPathList[[zip file "/data/app/com.google.android.gcm.demo.app-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.google.android.gcm.demo.app-1, /vendor/lib, /system/lib]]
09-06 00:16:41.480: E/AndroidRuntime(1246): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
09-06 00:16:41.480: E/AndroidRuntime(1246): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
09-06 00:16:41.480: E/AndroidRuntime(1246): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
09-06 00:16:41.480: E/AndroidRuntime(1246): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2400)
09-06 00:16:41.480: E/AndroidRuntime(1246): ... 10 more
(很抱歉,由于声誉不足,我无法发布图片。)
我正在使用以下模拟器
目标:Google API等级 - 18
并添加了Google帐户(我认为这不是必需的,因为我使用的是Android 4.3)。
在服务器端,我尝试构建WCF服务并发送HTTP POST请求,并得到以下响应:
“状态:200 - 好的 ID = 0:1378440485753994%921c249af9fd7ecd “
此外,我还尝试在helmibaraja.com使用GCM测试服务器,并获得以下响应
“的 {” multicast_id “:6425765079749845115,” 成功 “:1,” 失败 “:0,” canonical_ids “:0”,结果 “:[{” MESSAGE_ID “:” 0:1378441452317757%921c249af9fd7ecd“} ]} “
所以我认为服务器端不是问题。
我希望有人在客户端,模拟器设置或其他方面纠正我......
顺便说一下,我还将“ google-play-services_lib ”导入了我的包浏览器,并添加了“ gcm.jar ”和“ gcm-src。 jar “到我项目的Java Build Path。
提前致谢!
更正:点击演示客户端应用中的按钮会导致上述异常。
以下日志显示从服务器发送GCM的时间。
09-06 01:13:51.580: W/BroadcastQueue(274): Unable to launch app com.google.android.gcm.demo.app/10057 for broadcast Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10 pkg=com.google.android.gcm.demo.app (has extras) }: process is bad
09-06 01:13:51.610: W/GTalkService(466): [DataMsgMgr] broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.google.android.gcm.demo.app (has extras) }
答案 0 :(得分:0)
该框架无法找到广播接收者。你需要在你的清单文件中声明它。
在接收器标签内:
<receiver
android:name="com.google.android.gcm.demo.app.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="PACKAGE_NAME" />
</intent-filter>
</receiver>
答案 1 :(得分:0)
试试这个:
转到项目/属性/ Java构建路径/订单和导出 - 确保在Android Dependencies和支持库前面有一个检查,如果您使用它。标记所有复选框。单击应用并清理项目。
像这样添加:
<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.test.android" />
</intent-filter>
</receiver>
应该是:com.google.android.gcm.GCMBroadcastReceiver
而不是.GCMBroadcastReceiver
这对我有用。希望这有帮助。