2016-09-29 50 views
0

有一個應用程序,我正在重構使用服務而不是IntentService來處理活動更新。目前使用requestActivityUpdates來使用收集意圖並將其發送到基於IntentService的類進行處理的方法。使用服務請求活動更新 - 如何處理意圖

ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
      mGoogleApiClient, 
      DETECTION_INTERVAL_IN_MILLISECONDS, 
      getActivityDetectionPendingIntent() 
    ).setResultCallback(this); 

private PendingIntent getActivityDetectionPendingIntent() { 
    Intent intent = new Intent(getApplicationContext(), ActivityIntentService.class); 
    return PendingIntent.getService(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
} 

有沒有辦法直接在我的服務類處理的PendingIntent沒有傳遞給它關到IntentService?

回答

1

PendingIntent.getService()適用於IntentService和常規服務。只是

Intent intent = new Intent(getApplicationContext(), ActivityService.class); 

(或任何你服務的名稱)替換

Intent intent = new Intent(getApplicationContext(), ActivityIntentService.class); 

另請參見PendingIntent.getService(文檔):

檢索的PendingIntent,將啓動一個服務,就像調用Context.startService()。提供給服務的啓動參數將來自意圖的附加內容。

IntentService.onHandleIntent(Intent intent)收到的意向將在正常服務中收到onStartCommand(Intent intent, int flags, int startId)

+0

很棒!希望文檔更清楚一點... – MrMagoo

+0

@ T.Barrett現在好了記錄在這裏:-) –