谷歌云消息传递,无法初始化接收器。

4
我正在按照这里的教程进行操作:http://developer.android.com/google/gcm/gs.html,但是我在注册广播接收器时遇到了一些问题。
每当应用程序运行并且我成功注册设备并获取了注册ID后,就会出现以下错误:

java.lang.RuntimeException: 无法实例化接收器com.google.android.gcm.GCMBroadcastReceiver:java.lang.ClassNotFoundException:com.google.android.gcm.GCMBroadcastReceiver

以下是完整的堆栈跟踪信息:
06-19 01:08:03.994: E/AndroidRuntime(23867): 致命异常:主线程无法实例化接收者com.google.android.gcm.GCMBroadcastReceiver,因为找不到类com.google.android.gcm.GCMBroadcastReceiver。导致该异常的原因是java.lang.ClassNotFoundException: com.google.android.gcm.GCMBroadcastReceiver。
以下是完整清单,其中一些活动被删减了。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gotoohlala"
    android:versionCode="48"
    android:versionName="4.1.2" >

    <uses-sdk android:minSdkVersion="8"
        android:targetSdkVersion="11" />

    <uses-feature android:name="android.hardware.camera" />

    <supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:anyDensity="true" />

    <application
        android:hardwareAccelerated="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="com.google.android.maps" />

        <!--  Push notification -->
                <receiver
            android:name="pushNotification.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.gotoohlala" />
            </intent-filter>
        </receiver>

                <!--  Push notification END -->

        <activity
            android:name="launchOohlala.LaunchOohlala"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Light.NoTitleBar"
            android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="launchOohlala.FakeLogin"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Light.NoTitleBar"
            android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="launchOohlala.CheckEmail"
            android:label="@string/app_name"
            android:windowSoftInputMode="stateHidden|adjustResize"
            android:theme="@android:style/Theme.Light.NoTitleBar"
            android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name="discoverMyCampus.RulesFragment"
            android:label="@string/app_name"
            android:windowSoftInputMode="stateHidden|adjustResize"
            android:theme="@android:style/Theme.Light.NoTitleBar"
            android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden"  >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>


        <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.gotoohlala" />
            </intent-filter>
        </receiver>
        <service android:name=".GCMIntentService" />

        <activity android:name="com.facebook.LoginActivity"
                  android:theme="@android:style/Theme.Translucent.NoTitleBar"
                  android:label="@string/app_name" />
        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/applicationId" />

         <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyDP2zfbRiEH0mNCkn2lpxm92DQHsfIJfPU" />

    </application>

       <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />



    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <!-- Wifi related -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.SEND_SMS" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_CALENDAR" />
    <uses-permission android:name="android.permission.WRITE_CALENDAR" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />


    <uses-permission android:name="com.gotoohlala.gcm.permission.C2D_MESSAGE" />


    <permission android:name="com.gotoohlala.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="com.gotoohlala.permission.C2D_MESSAGE" /> 
    <!-- App receives GCM messages. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <!-- 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" />

    <uses-feature android:name="android.hardware.telephony" android:required="false" />
     <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <permission
        android:name="com.gotoohlala.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
    <uses-permission android:name="com.gotoohlala.permission.MAPS_RECEIVE"/>

</manifest>

我的GCMBroadcastReceiver类
package pushNotification;

import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.gotoohlala.OohlalaMain;

import discoverMyCampus.RulesFragment;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

/**
 * Handling of GCM messages.
 */
public class GCMBroadcastReceiver extends BroadcastReceiver {
    static final String TAG = "GCMDemo";
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    Context ctx;
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("SUCCESS SUCCESS", "ONRECEIVE HAS BEEN CALLED, WOOT WOOT!!");
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
        Log.d("SUCCESS SUCCESS", "ONRECEIVE HAS BEEN CALLED, WOOT WOOT!!");
        ctx = context;
        String messageType = gcm.getMessageType(intent);
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
            sendNotification("Send error: " + intent.getExtras().toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            sendNotification("Deleted messages on server: " +
                    intent.getExtras().toString());
        } else {
            sendNotification("Received: " + intent.getExtras().toString());
        }
        setResultCode(Activity.RESULT_OK);
    }

    // Put the GCM message into a notification and post it.
    private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager)
                ctx.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
                new Intent(ctx, RulesFragment.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(ctx)
        .setContentTitle("GCM Notification")
        .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.getNotification());
    }
}

有什么想法为什么我无法注册接收器吗? 谢谢。

你解决了吗? - Sudhanshu Gaur
2个回答

5
该应用程序正在寻找com.google.android.gcm.GCMBroadcastReceiver,但是您的类在另一个包中pushNotification.GCMBroadcastReceiver
您的应用程序包不同 - com.myAppName
尽管您的GCMBroadcastReceiver不必与应用程序包位于同一包中,但意图过滤器中的类别必须是应用程序的包。
您应该更改如下:
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="pushNotification" />
        </intent-filter>

转化为:

        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.myAppName" />
        </intent-filter>

编辑:

根据您的清单文件,您有其他错误:

您重复指定了广播接收器。如果您想使用自己的接收器,您应该删除这部分内容:

    <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.gotoohlala" />
        </intent-filter>
    </receiver>

另一个问题:
删除此行:
<uses-permission android:name="com.gotoohlala.gcm.permission.C2D_MESSAGE" />

既然您已经有了这些(正确的)代码行:

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

感谢您的澄清,但是在进行了推荐更改后,我仍然遇到相同的错误。还有一件事情我应该指出,就是我在手机上有一个测试服务器,用于向应用程序推送消息,但每当我尝试推送消息时,也会遇到相同的错误。 - hervens
好的,我添加了完整的清单,减去了一些链接活动。 - hervens

-1

尝试检查v.4库。 属性->Java构建路径->顺序和导出


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接