3

我目前正在努力添加Google API活動識別。ActivityRecognitionResult.hasResult(intent)在向意圖添加附加內容時爲false

我成功地讓活動使用掛起的意圖如報道所示,在樣品中:http://developer.android.com/training/location/activity-recognition.html

現在,我想從IntentService回調(由未決的意圖解僱)。要做到這一點我想在找到了解決辦法:Using ResultReceiver in Android

Intent intent = new Intent(ctx, ActivityRecognitionIntentService.class); 
intent.putExtra(ActivityRecognitionIntentService.REQUEST_RECEIVER_EXTRA, new ResultReceiver(null) { 
    @Override 
    protected void onReceiveResult(int resultCode, Bundle resultData) { 
     switch (resultCode) { 
      case ActivityRecognitionIntentService.RESULT_ID_WITH_ACTIVITYRESULT: 

       ActivityRecognitionResult result = resultData.getParcelable(ActivityRecognitionIntentService.RESULT_BUNDLE_ACTIVITYRESULT); 
       DetectedActivity mostProbableActivity = result.getMostProbableActivity(); 

       observer.onNext(mostProbableActivity); 

       // Get the confidence percentage for the most probable activity 
       int confidence = mostProbableActivity.getConfidence(); 

       // Get the type of activity 
       int activityType = mostProbableActivity.getType(); 

       Log.d(TAG, getNameFromType(activityType) + " confidence: " + confidence + " isMoving: " + isMoving(activityType)); 
       break; 
      case ActivityRecognitionIntentService.RESULT_ID_NO_ACTIVITYRESULT: 
       Log.e(TAG, "Nonfatal: no activity result"); 
       break; 
      default: 
       Log.e(TAG, "Unexpected resultCode: " + resultCode); 
     } 
    } 
}); 

這不幸導致ActivityRecognitionResult.hasResult(intent)總是返回false。

/** 
* Called when a new activity detection update is available. 
*/ 
@Override 
protected void onHandleIntent(Intent intent) { 

    // outputs "onHandleIntent false" 
    Log.d(TAG, "onHandleIntent " + ActivityRecognitionResult.hasResult(intent)); 

} 

進一步的測試表明,傳遞給意圖任何額外造成了同樣的問題,因此,例如:

Intent intent = new Intent(ctx, ActivityRecognitionIntentService.class); 
intent.putExtra("TEST", "TESTING"); 
PendingIntent pendingIntent = PendingIntent.getService(ctx, 0, intent, 
     PendingIntent.FLAG_UPDATE_CURRENT); 

ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(apiClient, 0, pendingIntent); 

再次使ActivityRecognitionResult.hasResult(intent)返回false。刪除「測試」額外,給出了預期的活動結果。

所以最後我的問題:

  • 爲什麼添加額外的意圖做出的意圖不能用於動作識別
  • 是否有可能在一些其他的方式來創建回調。 注意:這是爲了一個庫項目的目的,所以不能使用EventBus,因爲我沒有活動/片段來接收發布的事件。
+0

任何解決方案? ? – febaisi

+0

@febaisi以另一種方式解決它,請參閱下面的答案 – cYrixmorten

+0

這只是一種解決方法。對 ? 我認爲我們應該能夠以某種方式通過意圖傳遞一個可序列化或可分析的對象。 – febaisi

回答

相關問題