使用 Google 的 FIT API 跟踪当天的总步数
Tracking total number of steps for current day using Google's FIT API
我正在尝试使用 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())
}
根据 google 片段,我不得不暂停主线程,但这也会引发错误。所以我评论了代码,而是使用了 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[]
嗯....显然我在 AVD 上测试代码,GoogleFIT 没有安装在 AVD 中。即使一个安装 google 适合 AVD,步数数据也不会显示(我知道它不会被计算在内,但我希望它能跨设备同步)。我在物理设备上安装了该应用程序以及 Google FIT,它运行良好。
我正在尝试使用 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())
}
根据 google 片段,我不得不暂停主线程,但这也会引发错误。所以我评论了代码,而是使用了 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[]
嗯....显然我在 AVD 上测试代码,GoogleFIT 没有安装在 AVD 中。即使一个安装 google 适合 AVD,步数数据也不会显示(我知道它不会被计算在内,但我希望它能跨设备同步)。我在物理设备上安装了该应用程序以及 Google FIT,它运行良好。