无法实例化接收器GCMBroadcastReceiver

时间:2013-10-24 05:11:30

标签: java android eclipse android-intent android-manifest

我正在关注GCM的http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/指南。

目前,我在接收通知时遇到困难。 有时,当我运行我的代码时,它会因此错误而崩溃:

  
    

7.517:E / AndroidRuntime(8601):致命异常:主要     7.517:E / AndroidRuntime(8601):java.lang.RuntimeException:无法实例化接收器com.google.android.gcm.GCMBroadcastReceiver:java.lang.ClassNotFoundException:未找到类“com.google.android.gcm.GCMBroadcastReceiver” “在路径上:DexPathList [[zip文件”/data/app/com.example.someactivity-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.someactivity-1,/ vendor / lib, /系统/ lib中]]     7.517:E / AndroidRuntime(8601):在android.app.ActivityThread.handleReceiver(ActivityThread.java:2544)     7.517:E / AndroidRuntime(8601):在android.app.ActivityThread.access $ 1500(ActivityThread.java:153)     7.517:E / AndroidRuntime(8601):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1345)     7.517:E / AndroidRuntime(8601):在android.os.Handler.dispatchMessage(Handler.java:99)     7.517:E / AndroidRuntime(8601):在android.os.Looper.loop(Looper.java:137)     7.517:E / AndroidRuntime(8601):在android.app.ActivityThread.main(ActivityThread.java:5295)     7.517:E / AndroidRuntime(8601):at java.lang.reflect.Method.invokeNative(Native Method)     7.517:E / AndroidRuntime(8601):at java.lang.reflect.Method.invoke(Method.java:525)     7.517:E / AndroidRuntime(8601):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:739)     7.517:E / AndroidRuntime(8601):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)     7.517:E / AndroidRuntime(8601):at dalvik.system.NativeStart.main(Native Method)     7.517:E / AndroidRuntime(8601):引起:java.lang.ClassNotFoundException:未在路径上找到类“com.google.android.gcm.GCMBroadcastReceiver”:DexPathList [[zip file“/ data / app / com。 example.someactivity-1.apk“],nativeLibraryDirectories = [/ data / app-lib / com.example.someactivity-1,/ vendor / lib,/ system / lib]]     7.517:E / AndroidRuntime(8601):at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)     7.517:E / AndroidRuntime(8601):在java.lang.ClassLoader.loadClass(ClassLoader.java:501)     7.517:E / AndroidRuntime(8601):在java.lang.ClassLoader.loadClass(ClassLoader.java:461)     7.517:E / AndroidRuntime(8601):在android.app.ActivityThread.handleReceiver(ActivityThread.java:2539)

  

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.someactivity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <!-- GCM connects to Internet Services. -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <!-- Creates a custom permission so only this app can receive its messages. -->
    <permission
        android:name="com.example.someactivity.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.someactivity.permission.C2D_MESSAGE" />

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

    <!-- Network State Permissions to detect Internet status -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <!-- Permission to vibrate -->
    <uses-permission android:name="android.permission.VIBRATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock.Light" >
        <activity
            android:name="com.example.someactivity.Login"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
            <activity android:name="com.example.someactivity.fragmentContainer" >
        </activity>

        <receiver
            android:name="com.google.android.gcm.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="com.example.someactivity" />
            </intent-filter>
        </receiver>

        <service android:name=".GcmIntentService" />

    </application>

</manifest>     

我使用的接收器:

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String newMessage = intent.getExtras().getString("Some Message");
        // Waking up mobile if it is sleeping
        WakeLocker.acquire(getApplicationContext());

        /**
         * Take appropriate action on this message
         * depending upon your app requirement
         * For now i am just displaying it on the screen
         * */

        // Showing received message       
        Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();

        // Releasing wake lock
        WakeLocker.release();
    }
};

为什么我没有实例化接收器,我已经更改了包名称。

2 个答案:

答案 0 :(得分:3)

将gcm.jar添加到libs文件夹中。

答案 1 :(得分:1)

确保清单

中的接收者包名称正确无误