Google Fit(Android)获取最近10天的数据

时间:2016-07-21 05:57:42

标签: android google-fit

我正试图从Android(6.0)手机中的谷歌健身获取最近10天的数据。它正确地返回当前日期数据,但是当我尝试获取昨天或之前的数据时,它会返回随机或错误的数据。

    // get the start and end date of the urrent mobile
    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    long endTime = cal.getTimeInMillis();
    cal.add(Calendar.DAY_OF_YEAR, -10);
    long startTime = cal.getTimeInMillis();
    java.text.DateFormat dateFormat = DateFormat.getDateInstance();

    //-------------steps-------------------------------
    //code to get last 10 days steps
    DataReadRequest readRequest = new DataReadRequest.Builder()
    .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
    .bucketByTime(1, TimeUnit.DAYS)
    .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
    .build();

    DataReadResult dataReadResult=null;
    dataReadResult = Fitness.HistoryApi.readData(mGoogleApiClient, readRequest).await(1, TimeUnit.MINUTES);

    // display data
    //Used for aggregated data
    if (dataReadResult.getBuckets().size() > 0) {
        Log.e("History", "Number of buckets: " + dataReadResult.getBuckets().size());
        for (Bucket bucket : dataReadResult.getBuckets()) {
        List<DataSet> dataSets = bucket.getDataSets();
        for (DataSet dataSet : dataSets) {
            showDataSet(dataSet);
        }
        }
    }
    else if (dataReadResult.getDataSets().size() > 0) //Used for non-aggregated data
    {
        Log.e("History", "Number of returned DataSets: " + dataReadResult.getDataSets().size());
        for (DataSet dataSet : dataReadResult.getDataSets()) {
        showDataSet(dataSet);
        }
    }

请帮助我了解如何获得前几天的步骤。或者为什么我得到错误的数据。 感谢

3 个答案:

答案 0 :(得分:0)

使用自定义DataSource:

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中
DataReadRequest readRequest = new DataReadRequest.Builder()
            .aggregate(ESTIMATED_STEP_DELTAS, DataType.AGGREGATE_STEP_COUNT_DELTA)
            .bucketByActivityType(1, TimeUnit.SECONDS)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();

通过这种方式,您可以获得Google健身应用中显示的确切或“衍生”数据。

答案 1 :(得分:0)

您的代码唯一的问题是您的时间跨度不会像人们期望的那样从00:00:00开始,而是比当前时间偏移24小时。您可以轻松地验证是否在从dataReadResult.getBuckets()收到的存储桶上调用getStartTime()...

答案 2 :(得分:0)

private DataReadRequest dataReadRequest(){
    long startTime = 0;
    cal = Calendar.getInstance();
    int diff = printDifference(calendar_show.getTime(),cal.getTime());
    if (diff>0) {
        cal.add(Calendar.DATE, -diff);
        now = cal.getTime();
        cal.setTime(now);
    }
    long endTime = cal.getTimeInMillis();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    startTime = cal.getTimeInMillis();
    if (endTime==startTime){
        cal.set(Calendar.HOUR_OF_DAY, -1);
        startTime = cal.getTimeInMillis();
    }

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

    return readRequest;
}

获取步骤:

公共任务googlefitStpes(){

    DataReadRequest readRequest = dataReadRequest();

    return Fitness.getHistoryClient(getActivity(), GoogleSignIn.getLastSignedInAccount(getActivity()))
            .readData(readRequest)
            .addOnSuccessListener(new OnSuccessListener<DataReadResponse>() {
                @Override
                public void onSuccess(DataReadResponse dataReadResponse) {
                    if (dataReadResponse.getBuckets()!=null){
                        if (!dataReadResponse.getBuckets().isEmpty()){
                            List<Bucket> bucketList = dataReadResponse.getBuckets();
                            if (bucketList!=null){
                                if (!bucketList.isEmpty()){
                                    List<DataSet> dataSetList = bucketList.get(0).getDataSets();
                                    if (dataSetList!=null){
                                        if (!dataSetList.isEmpty()){
                                            for (Field field : dataSetList.get(0).getDataPoints().get(0).getDataType().getFields()) {
                                                GoogleFitSteps = Double.parseDouble(dataSetList.get(0).getDataPoints().get(0).getValue(field).toString());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {

                }
            });
}

这可能有帮助!。如果有缩短它的地方,请更正它。