Nexus 5-> Android 5.1.1上的NoClassFound com.google.android.gms.gcm.GcmReceiver

时间:2015-08-11 11:35:12

标签: java android eclipse

我正在尝试在eclipse中修改和实现GCM(hmkode)。我已经在eclipse中导入了项目并执行了必要的设置步骤。

http://hmkcode.com/android-google-cloud-messaging-tutorial/

在developer.google.com上浏览GCM示例。链接说google默认包含GcmReceiver类。我从hmkode示例中删除了旧的GcmBroadcastReceiver,并更改了GcmMessageHandler以扩展GcmListenerService而不是IntentService(在hmkode /原始代码中)。 / p>

链接: https://developers.google.com/cloud-messaging/android/client

问题: 当我尝试向客户端发送消息时,客户端崩溃,logcat中出现以下异常

E / AndroidRuntime(20573):java.lang.RuntimeException:无法实例化接收器com.google.android.gms.gcm.GcmReceiver:java.lang.ClassNotFoundException:未找到类“com.google” .android.gms.gcm.GcmReceiver“on path:DexPathList [[zip file”/data/app/com.hmkcode.android.gcm-1/base.apk"],nativeLibraryDirectories=[/vendor/lib,/ system / LIB] E / AndroidRuntime(20573):
在android.app.ActivityThread.handleReceiver(ActivityThread.java:2590)

我的班级结构 -

     public class GcmMessageHandler extends GcmListenerService {
     String mes;
     private Handler handler;
    public GcmMessageHandler() {
        super();
    }
//com.hmkcode.android.gcm.
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        handler = new Handler();
    }

    public void showToast(){
        handler.post(new Runnable() {
            public void run() {
                Toast.makeText(getApplicationContext(),mes , Toast.LENGTH_LONG).show();
            }
         });

    }



}

活动类

public class MainActivity extends Activity implements OnClickListener {

    Button btnRegId;
    EditText etRegId;
    GoogleCloudMessaging gcm;
    String regid;
    String PROJECT_NUMBER = "164502923904";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnRegId = (Button) findViewById(R.id.btnGetRegId);
        etRegId = (EditText) findViewById(R.id.etRegId);

        btnRegId.setOnClickListener(this);
    }
    public void getRegId(){
        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                String msg = "";
                try {
                    if (gcm == null) {
                        gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
                    }
                    regid = gcm.register(PROJECT_NUMBER);
                    msg = "Device registered, registration ID=" + regid;
                    Log.i("GCM",  msg);


                } catch (IOException ex) 
                {
                    msg = "Error :" + ex.getMessage();

                }
                return msg;
            }

            @Override
            protected void onPostExecute(String msg) {
                etRegId.setText(msg + "\n");
            }
        }.execute(null, null, null);
    }
    @Override
    public void onClick(View v) {
        getRegId();
    }

}

的Manifest.xml

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

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

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

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

    <application

        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
        >
        <activity
            android:name="com.hmkcode.android.gcm.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:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.hmkcode.android.gcm" />
            </intent-filter>
        </receiver>
        <service android:name="com.hmkcode.android.gcm.GcmMessageHandler" />

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

</manifest>

此外,gcm.jar已添加为依赖项并已导出。

1 个答案:

答案 0 :(得分:0)

如果您将Eclipse与常规的android gcm jar一起使用,则需要添加com.google.android.gcm.GcmReceiver而不是com.google.android.gcm.GCMBroadcastReceiver。这对我有用(至少是暂时的)