谷歌适合API:过去几天可用的卡路里,但今天没有,尽管今天有活动

时间:2016-02-15 12:19:19

标签: google-fit google-fit-sdk

以下是今天获取卡路里的代码:

    Calendar date = new GregorianCalendar();
    date.set(Calendar.HOUR_OF_DAY, 0);
    date.set(Calendar.MINUTE, 0);
    date.set(Calendar.SECOND, 0);
    date.set(Calendar.MILLISECOND, 0);
    long startTime = date.getTimeInMillis();

    date.set(Calendar.HOUR_OF_DAY, 23);
    date.set(Calendar.MINUTE, 59);
    date.set(Calendar.SECOND, 59);
    date.set(Calendar.MILLISECOND, 0);
    long endTime = date.getTimeInMillis();


    String calories;
    DataReadRequest dataReadRequest = new DataReadRequest.Builder().read(DataType.TYPE_CALORIES_EXPENDED).
            setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS).build();
    Fitness.HistoryApi.readData(App.gClient, dataReadRequest).setResultCallback(
            new ResolvingResultCallbacks<DataReadResult>(this.getActivity(), 0) {
                @Override
                public void onSuccess(DataReadResult dataReadResult) {
                    String calories;

                    DataSet dataSet = dataReadResult.getDataSet(DataType.TYPE_CALORIES_EXPENDED);
                    if (dataSet != null) {
                        List<DataPoint> dataPoint = dataSet.getDataPoints();
                        if (!dataPoint.isEmpty()) {
                            calories = dataPoint.get(0).getValue(Field.FIELD_CALORIES).toString();
                            txtCalories.setText(calories);
                        }
                    }


                }

                @Override
                public void onUnresolvableFailure(Status status) {

                }
            });

当我设置startDate = 1时,我能够看到卡路里计数。但是对于今天来说,没有统计数字。 开始时间是今天午夜12点,结束时间是今天晚上11:59。

1 个答案:

答案 0 :(得分:0)

今天的日子还不完整。因此,我认为结束时间会产生错误。相反,您应将结束时间设置为Math.min(System.getCurrentMillis(), date.getTimeInMillis();)