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

WearableListenerService触发应用时不显示启动屏的解决方案咨询

Wear OS启动屏在WearableListenerService唤起时不显示的解决方案

问题背景

根据Wear OS应用质量指南WO-V15及Play商店上架要求,Wear OS应用需集成启动屏。按官方文档完成启动屏与WearableListenerService的集成后,发现通过WearableListenerService唤起应用时,即使MainActivity的onCreate方法执行,启动屏仍无法显示。

用户提供的代码片段

WearableDataService实现

class WearableDataService : WearableListenerService() {
    private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)

    private val exerciseServiceRepository: ExerciseServiceRepository by inject()
    private val hapticFeedbackHelper: HapticFeedbackHelper by inject()

    @RequiresApi(Build.VERSION_CODES.S)
    override fun onDataChanged(dataEvents: DataEventBuffer) {
        dataEvents.forEach { dataEvent ->
            val uri = dataEvent.dataItem.uri
            when (uri.path) {
                WORKOUT_STATUS_PATH -> {
                    val item = DataMapItem.fromDataItem(dataEvent.dataItem)
                    item.dataMap.getByteArray(STATUS_DATA)?.let {
                        it.toWorkoutStatusUpdate().let { statusUpdate ->
                            scope.launch {
                       exerciseServiceRepository.updateWorkoutStatus(workoutStatusUpdate)
                            }
                            notifyUserWithVibrationThatSomethingHasChanged()
                        }
                    }
                }
            }
        }
        dataEvents.release()
        super.onDataChanged(dataEvents)
    }
}

启动屏实现代码

override fun onCreate(savedInstanceState: Bundle?) {
    // Handle the splash screen transition.
    val splash = installSplashScreen()
    super.onCreate(savedInstanceState)
}

清单文件配置

<activity
        android:name=".wearOs.MainActivity"
        android:exported="true"
        android:theme="@style/Theme.Starting"
        android:taskAffinity=".wearOs">

解决方案

  • 调整Activity启动模式与主题配置
    在MainActivity的清单配置中添加启动模式,确保任务栈处理正确,同时验证启动屏主题的完整性:

    <activity
            android:name=".wearOs.MainActivity"
            android:exported="true"
            android:theme="@style/Theme.Starting"
            android:taskAffinity=".wearOs"
            android:launchMode="singleTop">
    

    确认Theme.Starting继承自Wear OS专属主题(如Theme.Material3.Wearable.NoActionBar),且正确配置了windowSplashScreenAnimatedIcon、windowSplashScreenBackground等核心属性。

  • 显式启动Activity时添加正确的Intent标记
    如果在WearableDataService中主动唤起MainActivity,必须添加FLAG_ACTIVITY_NEW_TASK标记,必要时搭配FLAG_ACTIVITY_CLEAR_TOP确保Activity正常加载启动屏:

    // 在onDataChanged的对应逻辑中添加启动代码
    val intent = Intent(this, MainActivity::class.java).apply {
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
    }
    startActivity(intent)
    
  • 控制启动屏显示时长
    通过splash.setKeepOnScreenCondition延迟启动屏隐藏,避免因Service唤起时Activity初始化过快导致启动屏无显示:

    override fun onCreate(savedInstanceState: Bundle?) {
        val splash = installSplashScreen()
        // 设置启动屏保持显示的条件,示例为延迟2秒
        var keepShowing = true
        splash.setKeepOnScreenCondition { keepShowing }
        Handler(Looper.getMainLooper()).postDelayed({
            keepShowing = false
        }, 2000)
        super.onCreate(savedInstanceState)
        // 后续初始化逻辑
    }
    
  • 验证Service唤起逻辑
    在WearableDataService的onDataChanged方法中添加日志,确认触发时机正确,且没有其他逻辑拦截Activity的启动流程。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 03:35:24