【发布时间】:2021-01-20 23:26:03
【问题描述】:
假设我想构建一个应用程序,它定期请求当前位置(例如,每 10 分钟,这个数字应该是可配置的)并提交到服务器。
我知道对于这种情况,通常建议使用 Foreground Service 和 WorkManager。但是哪个更适合?以下是我的想法和疑问。
-
WorkManager - 主要用于保证执行的可延迟后台工作。但是我知道从 Android 8 (API 26) 开始引入后台位置,并且限制位置每小时仅更新几次https://developer.android.com/about/versions/oreo/background-location-limits。因此这可能不符合要求的定期更新。
-
ForegroundService - 非常适合运行并需要让用户意识到的东西。出于隐私目的,建议用于这种情况(位置跟踪)。 Google 还创建了一个示例应用程序来推广这种做法https://github.com/android/location-samples/tree/master/LocationUpdatesForegroundService。
从上面的分析看来,ForegroundService 就是那个。但是我还发现WorkManager 内置支持通过androidx.work.impl.foreground.SystemForegroundService https://developer.android.com/topic/libraries/architecture/workmanager/advanced/long-running#long-running-kotlin 与ForegroundService 一起使用Worker
这让我对我应该使用什么以及 Google 对这个特定场景真正推荐什么感到困惑。
有人知道吗?
【问题讨论】:
-
您介意分享您的决定吗?
标签: android android-architecture-components android-location android-workmanager foreground-service