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

WearOS如何检测硬件按键启动应用并跳转不同Compose页面?

解决Wear OS应用双击Home键唤醒跳转指定页面的问题

针对你用Jetpack Compose开发Wear OS应用时遇到的场景——冷启动能正常跳转,但应用在后台/前台时双击Home键直接恢复应用无法跳转到SOS页面——可以通过Wear OS专属的双击Home键Action API结合Activity的onNewIntent方法解决,步骤如下:

1. 在Manifest中注册双击Home键的Intent过滤器

给你的MainActivity添加专门接收双击Home键事件的Intent过滤器,这样系统会在用户双击自定义的Home键时向你的应用发送对应Intent:

<activity
    android:name=".MainActivity"
    android:exported="true">
    <!-- 原有应用启动器过滤器 -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <!-- 双击Home键事件过滤器 -->
    <intent-filter>
        <action android:name="com.google.android.wearable.action.DOUBLE_TAP_HOME" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

2. 处理Activity的onNewIntent方法

当应用处于后台/前台时,双击Home键不会触发onCreate,而是会调用onNewIntent。你需要在这里更新Intent并重新处理导航逻辑:

class MainActivity : ComponentActivity() {
    private lateinit var navController: NavHostController

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            WearAppTheme {
                navController = rememberNavController()
                NavHost(navController = navController, startDestination = getInitialRoute(intent)) {
                    composable(Screen.HomeScreen.route) { HomeScreen() }
                    composable(Screen.SosScreen.route) { SosScreen() }
                }
            }
        }
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        // 更新当前Activity的Intent为新接收的Intent
        intent?.let { setIntent(it) }
        // 根据新Intent跳转目标页面,避免重复跳转
        intent?.let {
            val targetRoute = getInitialRoute(it)
            if (navController.currentDestination?.route != targetRoute) {
                navController.navigate(targetRoute) {
                    // 清空返回栈,防止按返回键回到之前的页面(按需调整)
                    popUpTo(navController.graph.startDestinationId) { inclusive = true }
                }
            }
        }
    }

    private fun getInitialRoute(intent: Intent): String {
        return when {
            // 冷启动:点击应用图标
            intent.action == Intent.ACTION_MAIN && intent.hasCategory(Intent.CATEGORY_LAUNCHER) ->
                Screen.HomeScreen.route
            // 双击Home键唤醒
            intent.action == "com.google.android.wearable.action.DOUBLE_TAP_HOME" ->
                Screen.SosScreen.route
            // 其他默认场景(按需调整)
            else -> Screen.HomeScreen.route
        }
    }
}

关键说明

  • com.google.android.wearable.action.DOUBLE_TAP_HOME是Wear OS官方提供的双击Home键Action,只有注册对应的Intent过滤器,应用才能接收该事件
  • 调用setIntent(intent)是为了确保后续调用getIntent()能拿到最新的Intent,避免逻辑错误
  • 导航时判断当前页面是否为目标页面,防止重复执行跳转逻辑
  • popUpTo代码用于清空返回栈,如果你希望用户按返回键能回到之前的页面,可以移除这段配置

测试注意事项

  • 先在Wear OS设备的系统设置中,将双击Home键的自定义应用设置为你的应用
  • 测试后台场景:先按Home键将应用退到后台,再双击Home键验证是否跳转到SOS页面
  • 测试前台场景:应用处于前台时双击Home键,同样会触发跳转逻辑,符合需求

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 18:41:21