【问题标题】:Google Cloud Messaging Error: Unable to instantiate receiver谷歌云消息传递错误:无法实例化接收器
【发布时间】:2013-12-19 19:02:16
【问题描述】:

我已经使用 Google Play 服务按照关于新 GCM 的 google 指南进行操作,经过数小时的开发后,我设法在 GCM 服务器上注册了我的设备,以获取 REGISTER_ID 并创建一个正确发送发布请求的 php 脚本。

坏消息是我没有收到推送通知,当我运行我的应用程序时,logcat 显示此错误:

12-19 19:48:24.405: E/AndroidRuntime(9570): FATAL EXCEPTION: main
12-19 19:48:24.405: E/AndroidRuntime(9570): java.lang.RuntimeException: Unable to instantiate receiver com.baruckis.SlidingMenuImplementation.GcmBroadcastReceiver:
                                            java.lang.ClassNotFoundException: com.baruckis.SlidingMenuImplementation.GcmBroadcastReceiver
12-19 19:48:24.405: E/AndroidRuntime(9570):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2112)
12-19 19:48:24.405: E/AndroidRuntime(9570):     at android.app.ActivityThread.access$1500(ActivityThread.java:127)
12-19 19:48:24.405: E/AndroidRuntime(9570):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
12-19 19:48:24.405: E/AndroidRuntime(9570):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 19:48:24.405: E/AndroidRuntime(9570):     at android.os.Looper.loop(Looper.java:137)
12-19 19:48:24.405: E/AndroidRuntime(9570):     at android.app.ActivityThread.main(ActivityThread.java:4507)
12-19 19:48:24.405: E/AndroidRuntime(9570):     at java.lang.reflect.Method.invokeNative(Native Method)
12-19 19:48:24.405: E/AndroidRuntime(9570):     at java.lang.reflect.Method.invoke(Method.java:511)
12-19 19:48:24.405: E/AndroidRuntime(9570):     at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
12-19 19:48:24.405: E/AndroidRuntime(9570):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
12-19 19:48:24.405: E/AndroidRuntime(9570):     at dalvik.system.NativeStart.main(Native Method)
12-19 19:48:24.405: E/AndroidRuntime(9570): Caused by: java.lang.ClassNotFoundException: com.baruckis.SlidingMenuImplementation.GcmBroadcastReceiver
12-19 19:48:24.405: E/AndroidRuntime(9570):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
12-19 19:48:24.405: E/AndroidRuntime(9570):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-19 19:48:24.405: E/AndroidRuntime(9570):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-19 19:48:24.405: E/AndroidRuntime(9570):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2107)
12-19 19:48:24.405: E/AndroidRuntime(9570):     ... 10 more
12-19 19:48:52.620: I/Process(9570): Sending signal. PID: 9570 SIG: 9

这是我的清单(我已经省略了代码中不必要的部分):

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17"
    />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<permission android:name="com.baruckis.SlidingMenuImplementation.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.baruckis.SlidingMenuImplementation.permission.C2D_MESSAGE" />

<!-- This app has permission to register and receive data message. -->
<uses-permission
    android:name="com.google.android.c2dm.permission.RECEIVE" />

<application

               [...]

<receiver
        android:name="com.baruckis.SlidingMenuImplementation.GcmBroadcastReceiver"
        android:enabled="true"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.baruckis.SlidingMenuImplementation" />
        </intent-filter>
</receiver>

<service android:name="com.baruckis.SlidingMenuImplementation.GcmIntentService" />
<meta-data android:name="com.google.android.gms.version" android:value="4030500" />
    </application>

最后这是我的 GcmBroadcastReceiver 代码:

package com.baruckis.SlidingMenuImplementation;

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);
   }
}

谢谢大家! :)

【问题讨论】:

  • GcmBroadcastReceiver 代码中的包行在哪里?
  • 更新代码,抱​​歉忘记复制粘贴了!

标签: android compiler-errors broadcastreceiver runtime-error google-cloud-messaging


【解决方案1】:

尝试扩展提供的:com.google.android.gcm.GCMBroadcastReceiver

public class MyGCMBroadcastReceiver extends com.google.android.gcm.GCMBroadcastReceiver {
    protected String getGCMIntentServiceClassName(Context context) {
        return GCMIntentService.class.getName();
    }
}

【讨论】:

  • 我认为这不是一个可能的解决方案!我需要从 WakefulBroadcastReceiver 扩展...
  • 好的。尝试使用:android:name=".GcmBroadcastReceiver" 而不是 android:name="com.baruckis.SlidingMenuImplementation.GcmBroadcastReceiver"。我知道(根据经验)GCM 库代码对您的接收器类相对于您的意图服务的位置做出了一些假设。试试看。他们的示例显示了在清单中注册为相对包名称的接收器。
  • 你知道,我认为你应该给我的第一个答案。如果您查看 GCMBroadcastReceiver 的源代码,它会调用: GCMBaseIntentService.runIntentInService(context, intent, className);这是一个静态例程,在启动服务之前获取唤醒锁。所以你不需要 WakefulBroadcastReceiver 因为唤醒锁是在 onReceive 返回之前获取的。这是我使用的,没有任何问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-02
  • 2014-08-15
相关资源
最近更新 更多