无法实例化GcmBroadcastReceiver

时间:2013-12-02 11:09:33

标签: android google-cloud-messaging

根据Google手册https://developer.android.com/google/gcm/client.html#app

,我尝试使用GCM服务创建测试应用

但是当我收到消息时 - 我有这个错误:

  

无法实例化接收器   com.example.testpushnotification.GcmBroadcastReceiver:   java.lang.ClassNotFoundException:没找到类   " com.example.testpushnotification.GcmBroadcastReceiver"在路径上:   DexPathList [[zip文件   " /data/app/com.example.testpushnotification-2.apk"],   nativeLibraryDirectories = [/数据/ APP-LIB / com.example.testpushnotification-2,   / vendor / lib,/ system / lib]]

我一直在寻找所有相同的问题,但他们没有任何解决方案或解决方案没有帮助,或者他们已被弃用。

我的清单:

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


<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>  
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>

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


<application

    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version"/> 

    <activity
        android:name="com.example.testpushnotification.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=".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.example.testpushnotification"/>
        </intent-filter>
    </receiver>
    <service android:name=".GcmIntentService"/>
</application>

com.example.testpushnotification包中GcmBroadcastReceiver的源代码:

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);
    }

为什么Dex路径包含的包不同于我的?

请问,如何解决?

已修复已更改<receiver android:name=".GcmBroadcastReceiver" .. </receiver>

<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" .. </receiver>(案例敏感)

4 个答案:

答案 0 :(得分:8)

试试这个:

转到项目/属性/ Java构建路径/订单和导出 - 确保在Android Dependencies和支持库前面有一个检查,如果您使用它。标记所有复选框。单击应用并清理项目。

像这样添加:

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

应该是: com.google.android.gcm.GCMBroadcastReceiver 而不是 .GCMBroadcastReceiver

这对我有用。希望这有帮助。

答案 1 :(得分:2)

我关注了developer.android教程:/google/gcm/client.html

我的问题是我有&#34; Android Dependencies&#34;在属性 - > Java构建路径 - >订单和导出下打勾。

适当的库列表:

  • Android 4.4.2
  • Android私人图书馆
  • 机器人支撑-V4

希望这有助于那里的人!

此代码是正确的

<receiver
    android:name=".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.infostretch.iSOSAndroid" />
    </intent-filter>
</receiver>

答案 2 :(得分:1)

我遇到了同样的问题。但解决了它,让我分享我的解决方案步骤..

  • 我创建了一个新包,然后将BroadcastReceiver移动到该包。
  • 添加接收者声明TAG在为GCM registeredID提供正确包名的活动标记之后。            

我的主要问题是GcmBroadcastReceiver类,它扩展了WakefulBroadcastReceiver并且需要v4库。我出于某些原因添加了多个库,其中一个包含v4,我只是从该库中删除了v4。将v7作为库添加到我的应用程序中,然后清理并运行...然后它的工作原理。抱歉我的英语不好,我只是分享我的经验来帮助。

答案 3 :(得分:0)

Mi Fix是使用Manifest Editor Wizard来更改服务和接收者的名称。结果是:

<application
    android:allowBackup="true"
    android:icon="@drawable/journey_icon"
    android:logo="@drawable/journey_icon"
    android:permission="android.permission.INTERNET"
    android:theme="@style/AppTheme" >

    <meta-data 
        android:name="com.google.android.gms.version"           
        android:value="@integer/google_play_services_version" />

    <receiver         
        android:name=".GCMBroadcastReceiver"         
        android:permission="com.google.android.c2dm.permission.SEND" >         

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

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

已更改为

<application
    android:allowBackup="true"
    android:icon="@drawable/journey_icon"
    android:logo="@drawable/journey_icon"
    android:permission="android.permission.INTERNET"
    android:theme="@style/AppTheme" >

    <meta-data 
        android:name="com.google.android.gms.version"           
        android:value="@integer/google_play_services_version" />

    <receiver         
        android:name="journeyAux.GCMBroadcastReceiver"         
        android:permission="com.google.android.c2dm.permission.SEND" >         

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

    <service android:name="journeyAux.GCMIntentService" />