You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Health Connect数据需启动Google Fit才更新,是否需填写指定谷歌表单?

关于Google Health Connect步数同步及表单填写的问题

我需要在应用中展示用户当日步数,采用Google Health Connect实现,但查询StepsRecords时数据无法实时更新,只有启动Google Fit后才会同步。另外,我不确定是否必须填写谷歌的指定表单?

以下是我的实现代码:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val healthConnectClient = HealthConnectClient.getOrCreate(this)
        val someFlow = MutableStateFlow(0L)
        lifecycleScope.launch {
            someFlow.emit(
                readStepsByTimeRange(
                    healthConnectClient,
                    Instant.now().minus(Duration.ofDays(1)),
                    Instant.now()
                )
            )
        }
        setContent {
            Text(text = someFlow.collectAsState().value.toString())
        }
    }

    suspend fun readStepsByTimeRange(
        healthConnectClient: HealthConnectClient,
        startTime: Instant,
        endTime: Instant
    ): Long {
        return try {
            Log.d("TAG", "readStepsByTimeRange: begin")
            val response =
                healthConnectClient.readRecords(
                    ReadRecordsRequest(
                        recordType = StepsRecord::class,
                        timeRangeFilter = TimeRangeFilter.between(startTime, endTime)
                    )
                )

            Log.d("TAG", "readStepsByTimeRange: response $response")

            response.records.map {
                Triple(it.startTime, it.endTime, it.count)
            }.onEach {
                Log.d("TAG", "readStepsByTimeRange: $it")
            }
            response.records.sumOf { it.count }
        } catch (e: Exception) {
            Log.d("TAG", "readStepsByTimeRange: $e")
            return 0L
        }
    }
}

内容的提问来源于stack exchange,提问作者Tus Die

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 16:12:17