0

我正在尝试使用 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[]
4

1 回答 1

0

嗯....显然我正在测试 AVD 上的代码,而 Google FIT 没有安装在 AVD 中。即使有人在 AVD 中安装了 google fit,也不会显示步数数据(我知道它不会被计算在内,但我希望它能够跨设备同步)。我将该应用程序与 Google FIT 一起安装在物理设备上,并且可以正常工作。

于 2020-05-20T05:32:32.317 回答