2014-01-07 58 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調用的包更改! :)