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

如何在Jetpack Compose中检测用户闲置并实现可重置的超时定时器?

在Jetpack Compose中检测用户闲置状态并实现超时动作

要实现用户一段时间无操作后执行指定动作,同时在用户触发交互时重置计时,可以通过触摸事件监听 + 协程定时器的组合来完成,以下是具体实现方案:

核心思路

  1. 用Modifier.pointerInput监听界面上的所有触摸交互,每次触发交互就重置计时起点
  2. 借助LaunchedEffect启动协程定时器,在指定超时时间后执行目标动作;一旦用户触发交互,旧的定时器协程会被自动取消,同时启动新的定时器

完整代码示例

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import kotlinx.coroutines.delay
import android.widget.Toast

@Composable
fun IdleDetectionScreen() {
    // 定义闲置超时时间(单位:毫秒,这里设为5秒)
    val idleTimeoutMillis = 5000L
    // 用于触发定时器重置的key,每次触摸更新当前时间戳
    val resetTimerKey = remember { mutableStateOf(System.currentTimeMillis()) }
    // 标记当前是否处于闲置状态
    val isIdle = remember { mutableStateOf(false) }
    val context = LocalContext.current

    // 监听所有触摸事件,每次触摸就更新resetTimerKey,触发LaunchedEffect重启
    val interactionModifier = Modifier
        .fillMaxSize()
        .pointerInput(resetTimerKey.value) {
            awaitPointerEventScope {
                // 捕获所有触摸事件,只要有触摸就重置计时
                while (true) {
                    awaitPointerEvent()
                    resetTimerKey.value = System.currentTimeMillis()
                    isIdle.value = false
                }
            }
        }

    LaunchedEffect(resetTimerKey.value) {
        // 启动定时器,超时后执行动作
        delay(idleTimeoutMillis)
        isIdle.value = true
        // 这里替换成你需要执行的闲置动作,比如弹出提示、跳转页面等
        Toast.makeText(context, "用户已闲置5秒", Toast.LENGTH_SHORT).show()
    }

    // 界面内容示例
    Text(
        text = if (isIdle.value) "当前状态:闲置" else "当前状态:活跃",
        modifier = interactionModifier
    )
}

关键细节说明

  • pointerInput的使用:通过传入resetTimerKey.value作为key,每次触摸更新key值时,pointerInput的协程会重启,确保能持续监听触摸事件
  • LaunchedEffect的自动取消:当resetTimerKey.value更新时,旧的LaunchedEffect协程会被自动取消,新的协程重新启动计时,完美实现"触摸重置计时"的需求
  • 事件捕获范围:awaitPointerEventScope里的while(true)循环会持续捕获所有触摸事件,包括点击、滑动、长按等,确保任何用户交互都能触发重置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 07:57:28