【问题标题】:ActivityRecognitionResult.hasResult(intent) false when adding extras to the intentActivityRecognitionResult.hasResult(intent) 向意图添加额外内容时为 false
【发布时间】:2015-01-03 17:18:10
【问题描述】:

我目前正在添加 Google API 活动识别。

我成功地使用未决意图报告了活动,如示例中所示:http://developer.android.com/training/location/activity-recognition.html

现在,我想要来自 IntentService 的回调(由待处理的 Intent 触发)。为此,我尝试了在以下位置找到的解决方案: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) 返回错误。移除额外的“TEST”,会得到预期的 Activity 结果。

最后是我的问题:

  • 为什么向 Intent 添加 Extras 会使 Intent 无法用于活动识别
  • 是否可以通过其他方式创建回调。 注意:这是出于图书馆项目的目的,因此不能使用 EventBus,因为我没有 Activity/Fragment 来接收发布的事件。

【问题讨论】:

  • 任何解决方案? ?
  • @febaisi 以另一种方式解决了它,请参阅下面的答案
  • 这只是一种解决方法。正确的 ?我认为我们应该能够以某种方式通过意图传递可序列化或可打包的对象。
  • @febaisi 这是真的。我想这可能是为了防止篡改意图而设计的。

标签: android android-intent google-play-services intentservice


【解决方案1】:

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

实现可以在:https://github.com/mcharmas/Android-ReactiveLocation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    • 2021-12-17
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多