从谷歌健身获取数据睡眠

时间:2018-02-26 21:13:14

标签: android sleep google-fit

我需要谷歌健康阅读睡眠数据的帮助..

我有这个方法,但没有工作

private void accessGoogleFit(){
    Calendar cal = Calendar.getInstance();
    Date date = new Date();
    cal.setTime(date);

    long endTime = cal.getTimeInMillis();
    cal.add(Calendar.WEEK_OF_YEAR, -1);
    long startTime = cal.getTimeInMillis();

    //SessionReadRequest sessionReadRequest = new SessionReadRequest.Builder().read(DataType.AGGREGATE_ACTIVITY_SUMMARY).setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS).build();

    DataReadRequest dataReadRequest = new DataReadRequest.Builder().read(DataType.AGGREGATE_ACTIVITY_SUMMARY).setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS).build();

    Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this)).readData(dataReadRequest)
        .addOnSuccessListener(new OnSuccessListener<DataReadResponse>() {
        @Override
        public void onSuccess(DataReadResponse dataReadResponse) {

            List<DataSet> dataSets = dataReadResponse.getDataSets();
            for(DataSet dataSet: dataSets){
                for(Field field: dataSet.getDataType().getFields()){
                    if(field.getName().equalsIgnoreCase(FitnessActivities.SLEEP)){
                        Toast.makeText(MainActivity.this, "ENCONTRADO", Toast.LENGTH_LONG).show();
                    }
                }
            }
        }
    })
    .addOnFailureListener(new OnFailureListener() {
    @Override
        public void onFailure(@NonNull Exception e) {

        }
    });
}

有人有一个能够读取睡眠数据的例子吗?

1 个答案:

答案 0 :(得分:0)

尝试更改startTimeendTime,并按照此SO post中实施的代码进行操作。此github post中还有Fit API的完整睡眠历史示例。