2014-01-07 57 views
1

我正在关注android开发者的GCM Demo。得到以下情况。 GCMIntentService中的OnHandleIntent没有被调用。任何人都可以帮忙吗?onHandleIntent in GcmIntentService not getting called

package com.xyz.ads; 


import com.google.android.gms.gcm.GoogleCloudMessaging; 

import android.app.IntentService; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 

public class GcmIntentService extends IntentService { 
    public GcmIntentService() { 
     super("GcmIntentService"); 
     //super.onCreate(); 
     //super.onHandleIntent(); 
    } 

    public void onCreate() { 
     super.onCreate(); 

    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     super.onStartCommand(intent, startId, startId); 
     Log.i("LocalService", "Received start id " + startId + ": " + intent); 

     return START_STICKY; 
    } 
    public static final String TAG = "GCM Demo"; 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     Bundle extras = intent.getExtras(); 
     GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 
     // The getMessageType() intent parameter must be the intent you received 
     // in your BroadcastReceiver. 
     String messageType = gcm.getMessageType(intent); 

     // Release the wake lock provided by the WakefulBroadcastReceiver. 
     GcmBroadcastReceiver.completeWakefulIntent(intent); 
    } 
} 

这里是我的广播接收器

package com.xyz.ads; 

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()); 

     startWakefulService(context, (intent.setComponent(comp))); 
     setResultCode(Activity.RESULT_OK); 
    } 
} 

和我的清单

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

    <uses-sdk 
     android:minSdkVersion="11" 
     android:targetSdkVersion="19" /> 
    <!-- For Google Cloud Services --> 
    <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" /> 

    <!-- location --> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

    <permission 
     android:name="com.xyz.ads.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 
    <uses-permission android:name="com.xyz.ads.permission.C2D_MESSAGE" /> 
    <application 
     android:name="ADS" 
     android:allowBackup="true" 
     android:debuggable="true" 
     android:icon="@drawable/ic_launcher" 
     android:theme="@style/Theme.AppCompat" 
     > 
     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 

     <activity 
      android:name=".ui.DummyActivity" 
      android:label="@string/title_activity_dummy" > 
     </activity> 


     <!-- For Google Cloud Services --> 
     <receiver 
      android:name=".GcmBroadcastReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND" > 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
       <category android:name="com.xyz.ads" /> 
      </intent-filter> 
     </receiver> 

     <service 
      android:name="GcmIntentService" 
      android:enabled="true" > 
     </service> 

    </application> 

</manifest> 

的onReceive内接收器获取调用和意图服务的构造..

+0

有没有解决...我面临同样的问题...请分享解决方案... –

回答

6

我会说你缺乏类的名称点。

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

点代表您的应用程序的包名称。
假设你的包名是com.example.app。然后编写.GcmIntentService与编写com.example.app.GcmIntentService具有相同的效果。

1

试试这个

<receiver 
      android:name="com.xyz.ads.GcmBroadcastReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND" > 
      <intent-filter> 

       <!-- Receives the actual messages. --> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 

       <category android:name="com.xyz.ads" /> 
      </intent-filter> 
     </receiver> 

<service 
      android:name="com.xyz.ads.GcmIntentService" 
      android:enabled="true" > 
     </service> 
+1

我试过之前..它没有工作.. :(谢谢。 – lintu

1

我有同样的问题,但它不是代码(永久性像你的,像文档说:http://developer.android.com/google/gcm/client.html),这是Eclipse项目。

检查这一点,如果它可以帮助你:

做一个重构,并从一个包移动服务到另一个。检查androidManifest.xml是否不会自动更改包。如果没有,那么你有和我一样的问题。

我解决了它通过删除ServiceIntent类和创建一个新的(具有相同的内容)。看起来我的服务被断开或者类似的东西,并且当时我通过比较代码意识到我已经疯了。

随着新的,如果你再次检查,你会看到在androidmanifest.xml和你的服务由GcmBroadcastReceiver调用的包更改! :)