简体   繁体   中英

How to get system service in composable function?

我需要在可组合函数中注册 SensorManager 我使用 android 文档的这个link ,并且想像文档注册广播接收器一样注册 SensorManager 但我不知道如何获取系统服务以初始化 SensorManager。

@Composable
inline fun <reified T : Any> getRequiredService(): T {
    val context = LocalContext.current
    return requireNotNull(context.getSystemService())
}

// usage inside composable

val sensorManager = getRequiredService<SensorManager>()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM