【问题标题】:Steps Data receive from History api is not matched with google fit从 History api 接收的步骤数据与 google fit 不匹配
【发布时间】:2018-11-03 06:06:07
【问题描述】:

我希望在我的应用程序中计算 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);

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

【问题讨论】:

    标签: android google-api-client google-fit google-fit-sdk


    【解决方案1】:

    查看FAQ,它从字面上解释了你正在描述的行为。

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

    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,其他都一样。或者,您可以记录您自己的sessions - 这不仅仅是estimated_steps,而是具有唯一的会话标识符。

    【讨论】:

      【解决方案2】:

      我在我的应用程序中使用历史 API,并将输出与 Google FIT 应用程序进行比较,它们完全相同。

      val c = GregorianCalendar()
          c.set(Calendar.HOUR_OF_DAY, 0) //anything 0 - 23
          c.set(Calendar.MINUTE, 0)
          c.set(Calendar.SECOND, 0)
          var endTime = /*date.time + dayInMS*/(getCurrentUTCDate()*1000)
          var startTime = (ConfigVariable.appLastUpdateTime*1000) //converting last updated data from second to MS
          val readRequest = DataReadRequest.Builder()
                  .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
                  .bucketByTime(1  , TimeUnit.DAYS)
                  .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                  .build()
          val pendingResult = Fitness.HistoryApi.readData(mGoogleApiClient,readRequest)
          pendingResult.setResultCallback(this)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-07
        • 2023-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多