我正在尝试使用 Google 的 FIT API 获取当天的总步数。我正在使用来自 https://developers.google.com/android/reference/com/google/android/gms/fitness/HistoryClient的代码片段
这是我从该页面使用的特定代码片段
val currentTime = Calendar.getInstance()
val midNight = Calendar.getInstance()
val now = Date()
midNight.apply {
time = now
set(Calendar.MILLISECOND, 0)
set(Calendar.SECOND, 0)
set(Calendar.MINUTE, 0)
set(Calendar.HOUR, 0)
}
val response: Task<DataReadResponse> =
Fitness.getHistoryClient(context, GoogleSignIn.getLastSignedInAccount(context)!!)
.readData(
DataReadRequest.Builder()
.read(TYPE_STEP_COUNT_DELTA)
.setTimeRange(
midNight.timeInMillis,
currentTime.timeInMillis,
TimeUnit.MILLISECONDS
)
.bucketByTime(1, TimeUnit.DAYS)
.build()
)
// val readDataResult: DataReadResponse? = Tasks.await(response)
.addOnSuccessListener {
Log.d("Test", "Buckets "+it.buckets.toString())
for (bucket in it.buckets) {
Log.d("Test","Bucket "+bucket)
val dataSets = bucket.dataSets
for (dataSet in dataSets) {
Log.d("Test","dp's"+dataSet.dataPoints.toString())
for (dataPoint in dataSet.dataPoints) {
Log.d("Datapoint", dataPoint.toString())
}
}
}
}
.addOnFailureListener{
Log.d("Test","Failed "+it.toString())
}
根据谷歌代码片段,我不得不暂停主线程,但这也会引发错误。所以我已经注释了代码,而是使用了 onSuccessListener
桶中的数据是
Buckets [Bucket{startTime=1589814000000, endTime=1589818566423, activity=4, dataSets=[DataSet{d:step_count.delta:gms:overlay_explicit_input_local []}], bucketType=time, serverHasMoreData=true}]
Bucket Bucket{startTime=1589814000000, endTime=1589818566423, activity=4, dataSets=[DataSet{d:step_count.delta:gms:overlay_explicit_input_local []}], bucketType=time, serverHasMoreData=true}
但数据点是空的。(我想这是存储步骤的地方)
dp's[]