Wear OS 5下Flutter应用如何避免Ambient Mode被系统切后台?
问题:Wear OS 5下Ambient Mode应用被系统推至后台的解决方案需求
背景
我在Android Studio中使用Flutter开发适配Samsung Galaxy 7 ultra的倒计时应用,采用wear_plus包管理屏幕形态与Ambient Mode,应用的音效和计时功能均正常运行,设备Ambient Mode已开启,UI在交互/环境模式间切换时更新正常。
当前问题
Wear OS 5中,应用处于Ambient Mode一段时间后会被系统推至后台。我查阅了官方文档及论坛,了解到从Wear OS 5开始,系统会对处于Ambient Mode的常亮应用执行后台切换,官方建议使用Ongoing Activity API绑定持续任务通知,但我希望实现类似心率监测类健身APP的效果:允许应用在交互/Ambient Mode间切换,但处于Ambient Mode时不被切后台。
已尝试方案
我曾尝试用wakelock_plus保持屏幕交互模式,但这会阻止进入Ambient Mode;即使手动切换到Ambient Mode,几秒后仍会被系统切后台,目前已注释相关代码用于测试。
Flutter环境信息
flutter doctor -v [√] Flutter (Channel stable, 3.29.0, on Microsoft Windows [Version 10.0.19045.5487], locale en-US) [306ms] • Flutter version 3.29.0 on channel stable at C:\src\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 35c388afb5 (9 days ago), 2025-02-10 12:48:41 -0800 • Engine revision f73bfc4522 • Dart version 3.7.0 • DevTools version 2.42.2 [√] Windows Version (10 Pro 64-bit, 22H2, 2009) [1,594ms] [√] Android toolchain - develop for Android devices (Android SDK version 35.0.0) [2.1s] • Android SDK at C:\Users\Justin\AppData\Local\Android\sdk • Platform android-35-ext14, build-tools 35.0.0 • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java This is the JDK bundled with the latest Android Studio installation on this machine. To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`. • Java version OpenJDK Runtime Environment (build 21.0.5+-12932927-b750.29) • All Android licenses accepted. [√] Chrome - develop for the web [24ms] • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe [X] Visual Studio - develop Windows apps [23ms] X Visual Studio not installed; this is necessary to develop Windows apps. Download at https://visualstudio.microsoft.com/downloads/. Please install the "Desktop development with C++" workload, including all of its default components [√] Android Studio (version 2024.2) [21ms] • Android Studio at C:\Program Files\Android\Android Studio • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 21.0.5+-12932927-b750.29) [√] Connected device (4 available) [321ms] • sdk gwear x86 64 (mobile) • emulator-5554 • android-x64 • Android 14 (API 34) (emulator) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19045.5487] • Chrome (web) • chrome • web-javascript • Google Chrome 132.0.6834.197 • Edge (web) • edge • web-javascript • Microsoft Edge 133.0.3065.69 [√] Network resources [400ms] • All expected network resources are available. ! Doctor found issues in 1 category.
使用的依赖包
flutter_ringtone_manager: ^1.1.2 wear_plus: ^1.2.2 wakelock_plus: ^1.2.10 sensors_plus: ^6.1.1 hive_ce: ^2.10.1 hive_ce_flutter: ^2.2.0
测试代码
//WakelockPlus.enable();
内容的提问来源于stack exchange,提问作者Justin
相关产品推荐
相关产品推荐

