ActivityRecognitionResult.hasResult(intent) false 向意图添加额外内容时

ActivityRecognitionResult.hasResult(intent) false when adding extras to the intent

我目前正在努力添加 Google API Activity 识别。

我成功地使用待定意图报告了活动,如示例所示: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) 总是 return 错误。

/**
 * 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) return 错误。删除额外的 "TEST",得到预期的 Activity 结果。

最后我的问题是:

我找到了一个使用 IntentService 中的 LocalBroadcastReceiver 的解决方案,转发结果。

实现可以在以下位置找到:https://github.com/mcharmas/Android-ReactiveLocation