android - java.lang.RuntimeException : Unable to instantiate service com. 谷歌.android.gcm.GCMBroadCastReceiver

标签 android google-play-services google-cloud-messaging

我正在开发一个安卓应用。

我正在使用 GCM 实现推送通知。

但是,我无法从我的服务器接收推送通知。错误日志如下所示。

java.lang.RuntimeException: Unable to instantiate service
com.google.android.gcm.GCMBroadCastReceiver: java.lang.ClassNotFoundException: 
Didn't find class "com.google.android.gcm.GCMBroadCastReceiver" on path: DexPathList

AndroidManifest.xml 如下。

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

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="net.myapp.permission.RECEIVE" />

    <permission
        android:name="net.myapp.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="net.myapp.permission.C2D_MESSAGE" />

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

    <application
        android:name="net.myapp.sub.AppController"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/app_id" />

        <activity
            android:name="net.myapp.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>
        <activity
            android:name="net.myapp.DetailActivity"
            android:uiOptions="splitActionBarWhenNarrow" >
            <meta-data
                android:name="android.support.UI_OPTIONS"
                android:value="splitActionBarWhenNarrow" />

            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="twittercallback" />
            </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="net.myapp" />
            </intent-filter>
        </receiver>

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

    </application>

</manifest>

GcmBroadcastReceiver.java 在下面。

package net.myapp;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;

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

}

我确认我已正确导入 android-support-v4.jar 和 google-play-services.jar。 我尝试了 Google 搜索中的一些解决方案,但是,我还无法解决。

你能告诉我如何解决上述问题吗?

最佳答案

您在 list 中写道:

android:name="com.google.android.gcm.GCMBroadcastReceiver"

但是你的GcmBroadcastReceiver.javanet.myapp包中

android:name="com.google.android.gcm.GCMBroadcastReceiver" 更改为 android:name="net.myapp.GcmBroadcastReceiver"

关于android - java.lang.RuntimeException : Unable to instantiate service com. 谷歌.android.gcm.GCMBroadCastReceiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27314473/

相关文章:

android - Google Play 游戏服务包名称可能存在错误?

android - 在 Titanium 模块中包含 Google Play 服务

android - 如何禁用默认的 Firebase 崩溃报告?

android - 在没有通知负载的情况下在 Android 和 iOS 上使用 GCM

android - 导航栏与最后一个 ListView 项目重叠(Android)

android - 在 viewpager 中的 fragment 之间传递数据

android - Android : Google GCM vs. Amazon SNS 上的推送通知?

google-cloud-messaging - 谷歌云消息和更少的电池消耗

android - React-Native:当应用程序离线(无互联网访问)时,无法使用 firestore 添加或设置()文档

java - 从 android.media.Image 到 Mat