课程链接' Lcom / google / android / gms / gcm / GcmReceiver;'失败

时间:2016-03-07 12:04:02

标签: java android eclipse push-notification google-cloud-messaging

我在这个问题上待了两天

我试图在eclipse环境下将GCM集成到Android应用程序上。 一切都像魅力一样,但一旦我必须收到推送通知消息我

  

03-07 12:19:43.725:W / dalvikvm(4364):无法解决超类   LCOM /谷歌/机器人/克/ GCM / GcmReceiver; (331)03-07 12:19:43.725:   W / dalvikvm(4364):班级的链接   ' LCOM /谷歌/机器人/克/ GCM / GcmReceiver;'失败03-07 12:19:43.730:   D / AndroidRuntime(4364):关闭VM 03-07 12:19:43.730:   W / dalvikvm(4364):threadid = 1:线程退出未捕获的异常   (组= 0x411c42a0)03-07 12:19:43.825:E / AndroidRuntime(4364):致命   EXCEPTION:main 03-07 12:19:43.825:E / AndroidRuntime(4364):   java.lang.RuntimeException:无法实例化接收器   com.google.android.gms.gcm.GcmReceiver:   抛出java.lang.ClassNotFoundException:   com.google.android.gms.gcm.GcmReceiver

我的Google Play Lib已集成且效果很好,我的接收器已在清单中声明

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

    <uses-sdk
        android:minSdkVersion="7"
        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.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.android.vending.BILLING" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

    <application
        android:name="com.media.dailydeals.App"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
        <activity
            android:name="com.media.dailydeals.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.media.dailydeals"/>
            </intent-filter>
        </receiver>
        <service
            android:name="com.media.dailydeals.GcmBroadcastReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <activity
            android:name="com.facebook.LoginActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />
   <meta-data
            android:name="com.google.android.gms.analytics.globalConfigResource"
            android:resource="@xml/global_tracker" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/app_id" />

        <receiver
            android:name="com.google.android.gms.analytics.AnalyticsReceiver"
            android:enabled="true" >
            <intent-filter>
                <action android:name="com.google.android.gms.analytics.ANALYTICS_DISPATCH" />
            </intent-filter>
        </receiver>
        <service
            android:name="com.google.android.gms.analytics.AnalyticsService"
            android:enabled="true"
            android:exported="false" />


    </application>

</manifest>

我的GcmReceiver课程位于Google Play图书馆,因此我无法理解它是如何链接的。

我尝试用Jar lib做,我重建,清理项目,我甚至删除了重新安装的所有工作区。这是我最后一次拍摄

非常感谢任何帮助。

提前致谢。

3 个答案:

答案 0 :(得分:2)

我只是想在这里随心所欲地猜测问题是你的项目中没有包含support-v4库(因为com.google.android.gms.gcm.GcmReceiver扩展了android.support .v4.content.WakefulBroadcastReceiver)或您的Eclipse项目配置错误,如this answer中所述。

免责声明,我从未使用Eclipse,快速搜索#34;无法解析超类&#34;让我觉得这就是答案。

答案 1 :(得分:0)

申请中的通知意图是什么?如果只是为了通知和打开某些活动,你应该创建自己的BroadcastReceiver来调用服务并处理通知。

而不是使用:android:name =&#34; com.google.android.gms.gcm.GcmReceiver&#34;,使用:android:name =&#34; .util_gcm.GcmBroadcastReceiver&#34;。这将是你的BroadCast的方式。

例证BroadCast:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());

    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
}

}

此BroadCast调用GcmIntentService,用于处理通知。 :

public class GcmIntentService extends IntentService {
public static final String TAG = "Script";
private static final String PREF_NAME = "notification";


public GcmIntentService(){
    super("GcmIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(GcmIntentService.this);

    String title, cod, link,tipo, conteudo, messageType = gcm.getMessageType(intent);


    if(extras != null){
        if(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)){
            Log.i(TAG, "Error: "+extras.toString());
        }
        else if(GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)){
            Log.i(TAG, "Deleted: "+extras.toString());
        }
        else if(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)){

            title = extras.getString("titulo");

            link = extras.getString("link");
            tipo = extras.getString("tipo");


            //NotificationCustomUtil.sendNotification(GcmIntentService.this, title, cod,tipo);
                NotificationCustomUtil.sendNotification(GcmIntentService.this, title,tipo,link);
            }


        }
    }

    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

}

忘记在清单中声明服务:

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

我希望你有所帮助。 :)

答案 2 :(得分:0)

@Tunos,我们可以为您的Android回溯跟踪您的GCM设置。如果您已包含所有必需的GCM权限,请检查清单文件:

<manifest package="com.example.gcm" ...>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.example.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />

<application
...
/>
</manifest>

实现:

<service android:name=".RegistrationIntentService" android:exported="false"/>

接收者和消息处理程序:

<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.codepath.gcmquickstart" />
</intent-filter>
</receiver>

这些只是您在调试代码时应该检查的一些清单。以下是有关GCM的完整清单。另请参阅此document