0

我想了解GCM是如何工作的。爲此,我複製/粘貼http://developer.android.com/在「實施GCM客戶端」部分中提出的代碼。android-classnotfoundexception - 神祕

從服務器的工作原理髮送消息,但是當我的客戶端應用程序接收到該消息,它崩潰,告訴我有個:

E/AndroidRuntime(5722): java.lang.RuntimeException: Unable to instantiate receiver 
com.test.testnotification3.GcmBroadcastReceiver: java.lang.ClassNotFoundException: Didn't 
find class "com.test.testnotification3.GcmBroadcastReceiver" on path: 
/data/app/com.test.testnotification3-2.apk 

我搜索互聯網上類似的問題,很多人似乎由於其清單中存在問題(清單中包名錯誤)而導致此錯誤。但是,我檢查了我的表現,它似乎是確定:

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

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
<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.test.testnotification3.permission.C2D_MESSAGE" /> 

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

<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" /> 

    <receiver 
     android:name="com.test.testnotification3.GcmBroadcastReceiver" 
     android:permission="com.google.android.c2dm.permission.SEND" > 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 

      <category android:name="com.test.testnotification3" /> 
     </intent-filter> 
    </receiver> 

    <service android:name="com.test.testnotification3.GcmIntentService" /> 

    <activity 
     android:name="com.test.testnotification3.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> 
</application> 

如果您有任何想法... 非常感謝!

編輯: 這裏是我的GcmBroadcastReceiver:

package core; 

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

} 

當然,由於數據包名稱變了,我改爲 「com.test.testnotification3」 由 「芯」 的接收器。

+0

我們可以看到com.test.testnotification3.GcmBroadcastReceiver的源代碼嗎? –

+0

我沒有在您的清單中看到'com.test.testnotification3.permission.C2D_MESSAGE'權限的定義。你忘了將它包含在你的問題中,還是真的從清單中錯過了? – Eran

回答

1

您必須將您的Reciever放入另一個包中。如果系統位於主包裝上,系統將無法實例化。

+0

你說得對,那是墜機的原因。 但即使它沒有崩潰,我也沒有按照我應該得到的通知。但這是另一個問題,如果我找不到解決方案,我會回來!非常感謝! –