2012-08-07 224 views
6

我想學習如何使用谷歌的雲郵件系統設置一個客戶端接收器,我跟隨這名谷歌的教程:http://developer.android.com/guide/google/gcm/gs.html#android-app了java.lang.RuntimeException:無法實例化服務.GCMIntentService

我已經做了該指南要求,但是當我運行我的應用程序它的工作原理了一會兒,然後它崩潰,這是logcat的一切:

08-07 17:04:40.726: E/AndroidRuntime(8155): FATAL EXCEPTION: main 
08-07 17:04:40.726: E/AndroidRuntime(8155): java.lang.RuntimeException: Unable to instantiate service com.example.google.cloud.messaging.GCMIntentService: java.lang.ClassNotFoundException: com.example.google.cloud.messaging.GCMIntentService in loader dalvik.system.PathClassLoader[/data/app/com.example.google.cloud.messaging-1.apk] 
08-07 17:04:40.726: E/AndroidRuntime(8155):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:1933) 
08-07 17:04:40.726: E/AndroidRuntime(8155):  at android.app.ActivityThread.access$2500(ActivityThread.java:117) 
08-07 17:04:40.726: E/AndroidRuntime(8155):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:989) 
08-07 17:04:40.726: E/AndroidRuntime(8155):  at android.os.Handler.dispatchMessage(Handler.java:99) 
08-07 17:04:40.726: E/AndroidRuntime(8155):  at android.os.Looper.loop(Looper.java:130) 
08-07 17:04:40.726: E/AndroidRuntime(8155):  at android.app.ActivityThread.main(ActivityThread.java:3687) 
08-07 17:04:40.726: E/AndroidRuntime(8155):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-07 17:04:40.726: E/AndroidRuntime(8155):  at java.lang.reflect.Method.invoke(Method.java:507) 
08-07 17:04:40.726: E/AndroidRuntime(8155):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 
08-07 17:04:40.726: E/AndroidRuntime(8155):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
08-07 17:04:40.726: E/AndroidRuntime(8155):  at dalvik.system.NativeStart.main(Native Method) 
08-07 17:04:40.726: E/AndroidRuntime(8155): Caused by: java.lang.ClassNotFoundException: com.example.google.cloud.messaging.GCMIntentService in loader dalvik.system.PathClassLoader[/data/app/com.example.google.cloud.messaging-1.apk] 
08-07 17:04:40.726: E/AndroidRuntime(8155):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240) 
08-07 17:04:40.726: E/AndroidRuntime(8155):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551) 
08-07 17:04:40.726: E/AndroidRuntime(8155):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511) 
08-07 17:04:40.726: E/AndroidRuntime(8155):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:1930) 

這是我的代碼

public class MainActivity extends Activity { 

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

     GCMRegistrar.checkDevice(this); 
     GCMRegistrar.checkManifest(this); 
     final String regId = GCMRegistrar.getRegistrationId(this); 
     if (regId.equals("")) { 
      GCMRegistrar.register(this, "962129210868"); 
      Log.i("Registrando",""); 
     } else { 
      Log.i("Test", "Already registered"); 
     } 
    } 
} 

這是我的清單

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.google.cloud.messaging" 
    android:versionCode="1" 
    android:versionName="1.0" > 

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

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 

    <permission 
     android:name="com.example.google.cloud.messaging.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 

    <uses-permission android:name="com.example.google.cloud.messaging.permission.C2D_MESSAGE" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 

     <activity 
      android:name=".MainActivity" 
      android:label="@string/title_activity_main" > 
      <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.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.example.google.cloud.messaging" /> 
      </intent-filter> 
     </receiver> 

     <service android:name="com.example.google.cloud.messaging.GCMIntentService" android:enabled="true"/> 
    </application> 

</manifest> 

固定

我已創建一個新的類名爲GCMIntentService:

public class GCMIntentService extends GCMBaseIntentService { 

    @SuppressWarnings("hiding") 
    private static final String TAG = "GCMIntentService"; 

    public GCMIntentService() { 
     super("Test"); 
    } 

    @Override 
    protected void onRegistered(Context context, String registrationId) { 
     Log.i(TAG, "Device registered: regId = " + registrationId); 
    } 

    @Override 
    protected void onUnregistered(Context context, String registrationId) { 
     Log.i(TAG, "Device unregistered"); 
    } 

    @Override 
    protected void onMessage(Context arg0, Intent arg1) { 
     Log.d("GCM", "RECIEVED A MESSAGE"); 
     generateNotification(arg0, arg1.getStringExtra("message")); 
     } 

    @Override 
    protected void onDeletedMessages(Context context, int total) { 
     Log.i(TAG, "Received deleted messages notification"); 
    } 

    @Override 
    public void onError(Context context, String errorId) { 
     Log.i(TAG, "Received error: " + errorId); 
    } 

    @Override 
    protected boolean onRecoverableError(Context context, String errorId) { 
     // log message 
     Log.i(TAG, "Received recoverable error: " + errorId); 
     return super.onRecoverableError(context, errorId); 
    } 

    /** 
    * Issues a notification to inform the user that server has sent a message. 
    */ 
    private static void generateNotification(Context context, String message) { 
     int icon = R.drawable.ic_action_search; 
     long when = System.currentTimeMillis(); 
     NotificationManager notificationManager = (NotificationManager) 
       context.getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(icon, message, when); 
     String title = context.getString(R.string.app_name); 
     Intent notificationIntent = new Intent(context, GCMIntentService.class); 
     // set intent so it does not start a new activity 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
       Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent intent = 
       PendingIntent.getActivity(context, 0, notificationIntent, 0); 
     notification.setLatestEventInfo(context, title, message, intent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notificationManager.notify(0, notification); 
    } 

} 
+1

您是否在com.example.google.cloud.messaging包中包含GCMIntentService類? – 2012-08-07 15:24:51

+0

AAAAAAAA這是我錯過了。非常感謝。請回答在這個問題,所以我可以給你一個+1 :) – 2012-08-07 16:00:09

回答

9

你應該在客戶端代碼GCMIntentService,你應該按如下所示在AndroidManifest.xml文件中定義它。

<服務機器人:名字= 「package_name.GCMIntentService」/>

請注意,如果您使用GCMBroadcastReceiver意圖服務的名稱應爲GCMIntentService完全相同。如果您實施廣播接收器,則可以創建您的意向服務。

4

我有同樣的問題。而這個問題已經解決了,因爲你需要使用GCMIntentService類的名字。但是如果你想重新命名或者把這個類放到另一個包中,問題就會回來:)這裏的解決方案linksource code

解決方案:

我們應該創建接收器並設置我們的定製服務,以overrided方法getGCMIntentServiceClassName的名稱。

public class GCMReceiver extends GCMBroadcastReceiver { 
@Override 
protected String getGCMIntentServiceClassName(Context context) { 
    return "PCAKAGE_NAME.GCMService"; 
} 
} 

如果GCMIntentService位於另一個包中,PCKAGE_NAME是另一個包名稱。

也不要忘記在AndroidManifest.xml中更改接收者名稱。

相關問題