首页 > 解决方案 > 从 History api 接收的步骤数据与 google fit 不匹配

问题描述

我希望在我的应用程序中计算 google fit 步数,因为我使用的是由 google 提供的 History api。我发现从历史 api 接收的步骤与 google fit 不匹配,即使我使用了 google 提供的相同代码。下面是我的代码。

  Calendar cal = Calendar.getInstance();
        Date now = new Date();
        cal.setTime(now);
        long endTime = cal.getTimeInMillis();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        long startTime = cal.getTimeInMillis();

        DataSource ESTIMATED_STEP_DELTAS = new DataSource.Builder()
                .setDataType(DataType.TYPE_STEP_COUNT_DELTA)
                .setType(DataSource.TYPE_DERIVED)
                .setStreamName("estimated_steps")
                .setAppPackageName("com.google.android.gms")
                .build();

        DataReadRequest readRequest = new DataReadRequest.Builder()
                .aggregate(ESTIMATED_STEP_DELTAS, DataType.AGGREGATE_STEP_COUNT_DELTA)
                .aggregate(DataType.TYPE_DISTANCE_DELTA, DataType.AGGREGATE_DISTANCE_DELTA)
                .aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
                .bucketByTime(1, TimeUnit.DAYS)
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .build();

  Task<DataReadResponse> result = Fitness.getHistoryClient(getApplicationContext(),
                GoogleSignIn.getLastSignedInAccount(getApplicationContext())).readData(readRequest);

任何帮助将不胜感激,我长期以来一直困扰着这个问题,甚至没有从谷歌找到任何合适的教程。

标签: androidgoogle-api-clientgoogle-fitgoogle-fit-sdk

解决方案


请参阅常见问题解答,它从字面上解释了您正在描述的行为。

这可能是最接近的;假设设备上安装了最新的播放服务:

DataSource ESTIMATED_STEP_DELTAS = new DataSource.Builder()
   .setDataType(DataType.TYPE_STEP_COUNT_DELTA)
   .setType(DataSource.TYPE_DERIVED)
   .setStreamName("estimated_steps")
   .setAppPackageName("com.google.android.gms")
   .build();

DataReadRequest readRequest = new DataReadRequest.Builder()
   .aggregate(ESTIMATED_STEP_DELTAS, DataType.AGGREGATE_STEP_COUNT_DELTA)
   .aggregate(DataType.TYPE_DISTANCE_DELTA, DataType.AGGREGATE_DISTANCE_DELTA)
   .aggregate(DataType.TYPE_CALORIES_EXPENDED, DataType.AGGREGATE_CALORIES_EXPENDED)
   .aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
   .bucketByTime(1, TimeUnit.DAYS)
   .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
   .build();

它完全一样,除了TYPE_CALORIES_EXPENDED。或者,您可以记录自己的会话- 不仅是estimated_steps,而且具有唯一的会话标识符。


推荐阅读