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
相关产品推荐
相关产品推荐

