Android Studio 通过 Google Fit Android API 检索 的 每周 步 数 与 Google Fit 官方 应用 程序 中 显示 的 步 数 不 匹配

iyfamqjs  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(196)

我试图从Google Fit API检索用户的每周步数数据,但我从官方Google Fit应用程序数据中得到不同的步数结果。例如:通过google fit api检索到的周四步数是5244,而在google fit官方应用中,周四步数是5134。一周中每天的步数略有不同,但我希望得到确切的结果。我使用google fit历史API检索每周步数并显示在条形图中。我的代码如下所示:

fitnessOptions = FitnessOptions.builder()
            .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
            .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
            .addDataType(DataType.TYPE_LOCATION_SAMPLE, FitnessOptions.ACCESS_READ)
            .build();

    @RequiresApi(api = Build.VERSION_CODES.O)
private void invokeHistoryApiForWeeklySteps() {

    // Read the data that's been collected throughout the past week.
    ZonedDateTime endTime = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        endTime = LocalDateTime.now().atZone(ZoneId.systemDefault());
    }
    ZonedDateTime startTime = endTime.minusWeeks(1);

    Log.i(TAG, "Range Start: $startTime");
    Log.i(TAG, "Range End: $endTime");

    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)
            .bucketByTime(1, TimeUnit.DAYS)
            .setTimeRange(startTime.toEpochSecond(), endTime.toEpochSecond(), TimeUnit.SECONDS)
            .enableServerQueries()
            .build();

    Fitness.getHistoryClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
            .readData(readRequest)
            .addOnSuccessListener(response -> {

                // The aggregate query puts datasets into buckets, so convert to a
                // single list of datasets
                for (Bucket bucket : response.getBuckets()) {

                    //convert days in bucket to milliseconds
                    long days = bucket.getStartTime(TimeUnit.MILLISECONDS);
                    //convert milliseconds to date
                    Date stepsDate = new Date(days);
                    //convert date to day of the week eg: monday, tuesday etc
                    @SuppressLint("SimpleDateFormat")
                    DateFormat df = new SimpleDateFormat("EEE");
                    String weekday = df.format(stepsDate);

                    Log.i(TAG, stepsDate.toString());
                    theDates.add(weekday);

                    for (DataSet dataSet : bucket.getDataSets()) {
                        totalAvgSteps.add(dumpDataSet(dataSet));


                    }
                }
                Log.i(TAG, theDates.toString());
                Log.i(TAG, totalAvgSteps.toString());
                displayBarChart();
            })
            .addOnFailureListener(e ->
                    Log.w(TAG, "There was an error reading data from Google Fit", e));

}

    private int dumpDataSet(DataSet dataSet) {
    Log.i(TAG, "Data returned for Data type: ${dataSet.dataType.name}");
    int totalSteps = 0;
    for (DataPoint dp : dataSet.getDataPoints()) {
        Log.i(TAG, "Data point:");
        Log.i(TAG, "\tType: ${dp.dataType.name}");
        Log.i(TAG, "\tStart: ${dp.getStartTimeString()}");
        Log.i(TAG, "\tEnd: ${dp.getEndTimeString()}");

        for (Field field : dp.getDataType().getFields()) {
            String fieldName = field.getName();
            totalSteps += dp.getValue(field).asInt();

            Log.i(TAG, "\tfield: " + fieldName + "value: " + dp.getValue(field));

        }
    }
    return totalSteps;
}

下面是我在result中得到的代码:
我的代码结果:

这是谷歌飞度官方应用程序中的数据
Google Fit应用程序中的步数:

Google Fit应用程序中的步数:

vshtjzan

vshtjzan1#

所以我修正了步数差异的问题,并在这里分享,如果它也能帮助其他人的话。我犯的错误是我没有计算从昨天午夜12:00到上周午夜12:00的数据,这导致了每天步数的差异。下面是正确的日期和时间计算:

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

    cal.add(Calendar.WEEK_OF_MONTH, -1);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    long startTime = cal.getTimeInMillis();
    Log.i(TAG, String.valueOf(endTime));
    Log.i(TAG,String.valueOf(startTime) );

我们需要更改数据请求一点点,我在几秒钟内请求它,现在我在毫秒内请求,这是给我的确切数据作为谷歌适合官方应用程序.

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

剩下的代码和我在问题中发布的代码一样。

相关问题