如何从 Google Fit API 获取步骤?
How do I get the steps from Google Fit API?
对不起,我是 Android 和 Google API 的新手。我有以下连接到 GoogleFit 的代码。我还有一个 API 密钥和 Oauth。
Where/how 我是否使用 API 密钥和 Oauth?关于如何获取它们的许多指南,但关于将它们放置在何处的信息为零 them/how 在应用程序中使用它们。
以及我如何实际使用返回的步骤。我设置了全局:
private int steps;
然后尝试通过以下方式设置它:
steps = (int)total;
但它什么也没做。
这是函数的其余部分。我如何实际计算步数。
private void accessGoogleFit() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
long endtime = cal.getTimeInMillis();
cal.add(Calendar.YEAR, -1);
long starttime = cal.getTimeInMillis();
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();
Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
.readDailyTotal(DataType.TYPE_STEP_COUNT_DELTA)
.addOnSuccessListener(new OnSuccessListener<DataSet>() {
@Override
public void onSuccess(DataSet dataSet) {
Log.d("Status", "Success");
long total = dataSet.isEmpty()
? 0
: dataSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
Log.d("Steps ", String.valueOf(total));
steps = (int)total; //Trying to get steps here
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("Status", "Failure", e);
}
})
.addOnCompleteListener(new OnCompleteListener<DataSet>() {
@Override
public void onComplete(@NonNull Task<DataSet> task) {
Log.d("Status", "Complete");
}
});
}
我一直在查看官方文档和 Whosebug。但似乎 google 去年对 API 进行了重大更改,因此大多数内容都已过时,包括 google 自己的教程(2015 年发布)。还有几个更新文档的地方提供了代码片段,我不知道如何使用它们或放在哪里。
这是我提出请求的方式
Google Fit API : 请求 FitnessOptions like
FitnessOptions fitnessOptions = FitnessOptions.builder()
.addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.addDataType(DataType.TYPE_DISTANCE_DELTA, FitnessOptions.ACCESS_READ)
.addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.build();
您需要申请 GoogleSignIn.requestPermissions
还有我的请求函数
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
long endTime = cal.getTimeInMillis();
cal.add(Calendar.WEEK_OF_YEAR, -1);
long startTime = cal.getTimeInMillis();
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
// .read(DataType.TYPE_STEP_COUNT_DELTA)
.bucketByTime(8, TimeUnit.DAYS)
.enableServerQueries()
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
Fitness.getHistoryClient(
this,
GoogleSignIn.getLastSignedInAccount(this))
.readData(readRequest)
.addOnSuccessListener(new OnSuccessListener<DataReadResponse>() {
@Override
public void onSuccess(DataReadResponse dataReadResponse) {
Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.toString());
Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.getStatus());
Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.getDataSet(DataType.TYPE_STEP_COUNT_DELTA));
Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.getBuckets().get(0));
Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.getBuckets().get(0).getDataSets().size());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("TAG_F", "onFailure: 1 " + e.getMessage());
}
})
.addOnCompleteListener(new OnCompleteListener<DataReadResponse>() {
@Override
public void onComplete(@NonNull Task<DataReadResponse> task) {
Log.d("TAG_F", "onComplete: 1 ");
}
});
对不起,我是 Android 和 Google API 的新手。我有以下连接到 GoogleFit 的代码。我还有一个 API 密钥和 Oauth。
Where/how 我是否使用 API 密钥和 Oauth?关于如何获取它们的许多指南,但关于将它们放置在何处的信息为零 them/how 在应用程序中使用它们。
以及我如何实际使用返回的步骤。我设置了全局:
private int steps;
然后尝试通过以下方式设置它:
steps = (int)total;
但它什么也没做。
这是函数的其余部分。我如何实际计算步数。
private void accessGoogleFit() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
long endtime = cal.getTimeInMillis();
cal.add(Calendar.YEAR, -1);
long starttime = cal.getTimeInMillis();
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();
Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
.readDailyTotal(DataType.TYPE_STEP_COUNT_DELTA)
.addOnSuccessListener(new OnSuccessListener<DataSet>() {
@Override
public void onSuccess(DataSet dataSet) {
Log.d("Status", "Success");
long total = dataSet.isEmpty()
? 0
: dataSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
Log.d("Steps ", String.valueOf(total));
steps = (int)total; //Trying to get steps here
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("Status", "Failure", e);
}
})
.addOnCompleteListener(new OnCompleteListener<DataSet>() {
@Override
public void onComplete(@NonNull Task<DataSet> task) {
Log.d("Status", "Complete");
}
});
}
我一直在查看官方文档和 Whosebug。但似乎 google 去年对 API 进行了重大更改,因此大多数内容都已过时,包括 google 自己的教程(2015 年发布)。还有几个更新文档的地方提供了代码片段,我不知道如何使用它们或放在哪里。
这是我提出请求的方式 Google Fit API : 请求 FitnessOptions like
FitnessOptions fitnessOptions = FitnessOptions.builder()
.addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.addDataType(DataType.TYPE_DISTANCE_DELTA, FitnessOptions.ACCESS_READ)
.addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.build();
您需要申请 GoogleSignIn.requestPermissions
还有我的请求函数
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
long endTime = cal.getTimeInMillis();
cal.add(Calendar.WEEK_OF_YEAR, -1);
long startTime = cal.getTimeInMillis();
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
// .read(DataType.TYPE_STEP_COUNT_DELTA)
.bucketByTime(8, TimeUnit.DAYS)
.enableServerQueries()
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
Fitness.getHistoryClient(
this,
GoogleSignIn.getLastSignedInAccount(this))
.readData(readRequest)
.addOnSuccessListener(new OnSuccessListener<DataReadResponse>() {
@Override
public void onSuccess(DataReadResponse dataReadResponse) {
Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.toString());
Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.getStatus());
Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.getDataSet(DataType.TYPE_STEP_COUNT_DELTA));
Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.getBuckets().get(0));
Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.getBuckets().get(0).getDataSets().size());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("TAG_F", "onFailure: 1 " + e.getMessage());
}
})
.addOnCompleteListener(new OnCompleteListener<DataReadResponse>() {
@Override
public void onComplete(@NonNull Task<DataReadResponse> task) {
Log.d("TAG_F", "onComplete: 1 ");
}
});