2014-01-13 38 views
0

我正在嘗試在我的android應用程序中集成G​​SM。我能夠運行我的應用程序並通過它註冊設備。無法在Android中註冊GCM的應用程序

執行我的自定義的接收方法「GCMReceiver.getGCMIntentServiceClassName」,但是當GCM嘗試實例我GCM服務(TestService的)我得到 上的logcat如下:

「無法啓動服務意向{行動=融爲一體。 google.android.c2dm.intent.REGISTRATION flg = 0x10 pkg = test.example.app cmp = test.example.app/com.activate.gcm.GCMIntentService not found「

任何想法?

我的活動代碼:

GCMRegistrar.checkDevice(this); 
GCMRegistrar.checkManifest(this); 
GCMRegistrar.register(getApplicationContext(), "<SENDER_ID>");    

Android清單:

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

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

    <permission android:name="test.example.app.permission.C2D_MESSAGE" android:protectionLevel="signature" /> 

    <uses-permission android:name="test.example.app.permission.C2D_MESSAGE" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="test.example.app.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="test.example.app.GCMReceiver" 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="test.example.app" /> 
      </intent-filter> 
     </receiver> 

     <service android:name=".TestService" android:enabled="true" /> 
    </application> 

</manifest> 

GCMReceiver類:

package test.example.app; 
import android.content.Context; 
import android.widget.Toast; 

import com.google.android.gcm.GCMBroadcastReceiver; 

public class GCMReceiver extends GCMBroadcastReceiver {  
    @Override 
    public String getGCMIntentServiceClassName(Context context) { 
     return "test.example.app.TestService"; 
    } 
} 

TestService的類:

package test.example.app; 

public class TestService extends GCMBaseIntentService { 

    public TestService() { 
     super("SENDER_ID"); 
    } 

    @Override 
    protected void onError(Context arg0, String arg1) { 

    } 

    @Override 
    protected void onMessage(Context arg0, Intent arg1) { 

    } 

    @Override 
    protected void onRegistered(Context arg0, String arg1) { 

    } 

    @Override 
    protected void onUnregistered(Context arg0, String arg1) { 
     // TODO Auto-generated method stub 
    } 
} 
+0

你的TestService類放置在哪裏?這是主包還是子包? – Saqib

+0

'com.activate.gcm.GCMIntentService'從哪裏來?它沒有在您發佈的代碼中的任何地方定義。也許你重新命名了一些你的類,並沒有正確地重建項目。嘗試清理並重新構建。 – Eran

+0

只是爲了檢查,我已經創建了一個新項目,只有Activity + TestService + GCMReceiver +我使用了相同的Android Manifest 。 所有的類都放在同一個包中,我仍然收到相同的錯誤消息。 順便說一句,我已經下載google-play-services_lib,將它添加到我的PackageExplorer並從我的項目中引用它,以便能夠使用GCMRegistrar – user3190343

回答

0

嘗試將TestService名稱更改爲GCMIntentService(重命名它)。