编程语言
首页 > 编程语言> > java-无法实例化接收器GCMBroadcastReceiver

java-无法实例化接收器GCMBroadcastReceiver

作者:互联网

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

目前,我在接收通知方面陷入困境.
有时,当我运行代码时,它会因以下错误而崩溃:

7.517: E/AndroidRuntime(8601): FATAL EXCEPTION: main
7.517: E/AndroidRuntime(8601): java.lang.RuntimeException: Unable to instantiate receiver com.google.android.gcm.GCMBroadcastReceiver: java.lang.ClassNotFoundException: Didn’t find class “com.google.android.gcm.GCMBroadcastReceiver” on path: 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 android.app.ActivityThread.handleReceiver(ActivityThread.java:2544)
7.517: E/AndroidRuntime(8601): at android.app.ActivityThread.access$1500(ActivityThread.java:153)
7.517: E/AndroidRuntime(8601): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345)
7.517: E/AndroidRuntime(8601): at android.os.Handler.dispatchMessage(Handler.java:99)
7.517: E/AndroidRuntime(8601): at android.os.Looper.loop(Looper.java:137)
7.517: E/AndroidRuntime(8601): at 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): Caused by: java.lang.ClassNotFoundException: Didn’t find class “com.google.android.gcm.GCMBroadcastReceiver” on path: 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): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
7.517: E/AndroidRuntime(8601): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
7.517: E/AndroidRuntime(8601): at 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();
    }
};

为什么我的接收器未实例化,我已经更改了程序包名称.

解决方法:

将gcm.jar添加到您的libs文件夹.

标签:eclipse,android-intent,android-manifest,java,android
来源: https://codeday.me/bug/20191030/1966665.html