2014-01-13 39 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(重命名它)。