android - Google Cloud Messaging错误: Unable to instantiate receiver

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

我已经遵循有关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

这是我的 list (我省略了不必要的代码部分):
<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);
   }
}

谢谢大家! :)

最佳答案

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

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

关于android - Google Cloud Messaging错误: Unable to instantiate receiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20689643/

相关文章:

java - 尝试调用android接口(interface)方法

android - 如何在新的 NavigationView 中创建一个简单的分隔线?

android - 如何为信息 View 适配器调用项目点击监听器?

c++ - Def文件和cpp文件编译

compilation - 如何在Windows上的SCons中导出具有备用扩展名的程序

Android:BroadcastReceiver 中的 CountDownTimer

android - Android 中的 3D 轮播

compiler-errors - 我的Cython代码解析为C,但无法编译。第一次尝试使用外部C代码

android - 对 BroadCast SMS Intent 的权限拒绝

Android - 为两个 Intent 注册广播接收器?