[英]Best way get GPS Location in background for Android API level 30 and higher
My application determines the speed limit by the user's location and tells the user if he has exceeded it.我的应用程序根据用户的位置确定速度限制,并告诉用户他是否超过了它。 Starting with Android API level 30 and higher, Google has defined IntentService as deprecated and suggests using WorkManager or JobIntentService and also states that it is necessary to migrate from Firebase JobDispatcher to WorkManager.从 Android API 级别 30 及更高级别开始,Google 已将 IntentService 定义为已弃用,并建议使用WorkManager或JobIntentService ,并声明有必要从Z035489FF8D092741943E4A8324 迁移到 WorkManager。 I see two ways to solve this problem:我看到了两种解决这个问题的方法:
I'm worried about:我对某事感到担心:
I would prefer going with second option, it gives you more time between each rescheduling of the Worker
.我更喜欢使用第二个选项,它可以让您在每次重新安排Worker
之间有更多时间。
Regarding your concerns:关于您的担忧:
Worker
are put in the database and are executed independently of the application. Scheduled Worker
被放入数据库并独立于应用程序执行。 Which means, user-visibility of the application has no effect.这意味着,应用程序的用户可见性没有任何影响。 In your case, I presume you want to cancel your scheduled work once the user resumes the application, that being said you can assign a tag
to the Worker
and purge any scheduled or on-going once visible to the user.在您的情况下,我假设您想在用户恢复应用程序后取消您的计划工作,也就是说您可以为Worker
分配一个tag
,并清除任何对用户可见的计划或正在进行的工作。Worker
s isolated from the MVVM and just inject Use cases/interactors in the worker and do execute the use case/query the interactor.我更喜欢保持Worker
与 MVVM 隔离,并且只在 Worker 中注入用例/交互器并执行用例/查询交互器。 WorkerManager
offers fine APIs to query the Worker
status, you might need to write common ground between your previous implementation and for API >= 30. Treat the Worker
as different execution container for your use case. WorkerManager
提供了很好的 API 来查询Worker
状态,您可能需要在之前的实现和 API >= 30 之间编写共同点。将Worker
视为您的用例的不同执行容器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.