1

我最近开始了解 Android GCM。我在这里尝试 Android Developer 中给出的 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 - OK id=0:1378440485753994%921c249af9fd7ecd

此外,我还尝试在helmibaraja.com上使用 GCM 测试服务器,并得到以下响应

" {"multicast_id":6425765079749845115,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1378441452317757%921c249af9fd7ecd"}]} "

所以我认为服务器端不是问题。

我希望有人在客户端,模拟器设置等上纠正我......

顺便说一句,我还将“ google-play-services_lib ”导入到我的包资源管理器中,并将“ gcm.jar ”和“ gcm-src.jar ”添加到我项目的 Java 构建路径中。

提前致谢!

更正:上述异常是在演示客户端应用程序中单击按钮时引起的。

以下日志显示从服务器发送 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) }
4

2 回答 2

0

The framework is not able to find the broadcastreceiver. you need to declare that in your manifest file.

inside the receiver tag:

<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>
于 2013-09-06T04:44:09.617 回答
0

尝试这个:

转到 Project/Properties/Java Build Path/Order and Export -- 如果您使用它,请确保在 Android 依赖项和支持库前面有一个检查。标记所有复选框。单击应用并清理项目。

像这样添加:

<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

这对我有用。希望这会有所帮助。

于 2017-05-09T05:55:26.843 回答