2015-03-31 35 views
-1

我可以從GCM服務器獲得註冊ID,趕上了與gcm-alert Push Notification online tool發送通知,但GcmIntentService不會被調用的onHandleIntent方法。
有一個有趣的事情,onDestroy方法被調用。任何幫助將不勝感激。

GcmIntentService.java
的Android onHandleIntent從未被稱爲

package ondermerol.com.studiopushtest; 

import android.app.IntentService; 
import android.content.Intent; 
import android.content.Context; 
import android.util.*; 
/** 
* An {@link IntentService} subclass for handling asynchronous task requests in 
* a service on a separate handler thread. 
* <p/> 
* TODO: Customize class - update intent actions, extra parameters and static 
* helper methods. 
*/ 
public class GcmIntentService extends IntentService { 

    private final static String Tag="---IntentServicetest"; 

    public GcmIntentService() { 
     // TODO Auto-generated constructor stub 
     super("GcmIntentService"); 
     Log.d(Tag, "Constructor"); 
    } 
    @Override 
    public void onDestroy() { 
     Log.d(Tag, "onDestroy()"); 
     super.onDestroy(); 
    } 
    @Override 
    public void onStart(Intent intent, int startId) { 
     Log.d(Tag, "onStart()"); 
     super.onStart(intent, startId); 
    } 

    @Override 
    public void setIntentRedelivery(boolean enabled) { 
     Log.d(Tag, "setIntentRedelivery()"); 
     super.setIntentRedelivery(enabled); 
    } 
    @Override 
    protected void onHandleIntent(Intent intent) { 
     // TODO Auto-generated method stub 
     Log.d(Tag, "IntentServicetest is onHandleIntent!"); 

    } 



GcmBroadcastReceiver.java

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { 

    public GcmBroadcastReceiver() { 
    } 

    // Receives the broadcast directly from GCM service 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     // TODO Auto-generated method stub 
     // Explicitly specify that GcmIntentService will handle the intent. 
     ComponentName comp = new ComponentName(context.getPackageName(), 
       ondermerol.com.studiopushtest.GcmIntentService.class.getName()); 
     // Start the service, keeping the device awake while it is launching. 
     startWakefulService(context, (intent.setComponent(comp))); 
     setResultCode(Activity.RESULT_OK); 
    } 
} 



AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="ondermerol.com.studiopushtest" > 

    <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="ondermerol.com.studiopushtest.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 
    <uses-permission 
     android:name="ondermerol.com.studiopushtest.permission.C2D_MESSAGE" /> 

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

    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".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=".GcmBroadcastReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND" > 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RETRY" /> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 

       <category android:name="ondermerol.com.studiopushtest" /> 
      </intent-filter> 
     </receiver> 

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

    </application> 

</manifest> 
+1

如果需要,我可以給其他兩個類。 – ondermerol 2015-03-31 07:12:05

+0

你怎麼知道它不叫? – 2015-03-31 07:12:34

+0

我在GcmIntentService的evey方法上放置了斷點,並且看到它沒有被調用,儘管它的其他方法被調用。 – ondermerol 2015-03-31 07:13:57

回答

0

我看到它被調用,但該方法中的斷點永遠不會工作,因爲IntentService在另一個線程中工作。
onDestroy被調用是因爲IntentService在從onHandleIntent()返回後自行停止。
感謝您的回答Service.onDestroy() is called directly after creation, anyway the Service does its work

+1

「該方法中的斷點永遠不會工作,因爲IntentService在另一個線程中工作」好吧,這是不正確的。我在IntentService#onHandleIntent()中添加了斷點,Android Studio停止了執行,所以它可以在主線程以外的線程中使用斷點。這個問題很可能是由onStartCommand()覆蓋造成的。 – aga 2015-03-31 08:02:34

+0

我刪除了onStartCommand()覆蓋並且沒有改變。也許我的測試設備(Samsung Galaxy Note 2)或android studio有問題。 – ondermerol 2015-03-31 08:53:58

1

從文檔上IntentService#onStartCommand(Intent intent, int flags, int startId)

/** 
* You should not override this method for your IntentService. Instead, 
* override {@link #onHandleIntent}, which the system calls when the IntentService 
* receives a start request. 
* @see android.app.Service#onStartCommand 
*/ 

我看到,在您的實現你重寫onStartCommand(),儘量堅持使用默認一。

+0

非常感謝您的建議,我解決了這個問題,我必須更頻繁地使用日誌:) – ondermerol 2015-03-31 07:52:34