简体   繁体   English

获得 GPS Android API 级别 30 及更高级别的后台位置的最佳方法

[英]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 定义为已弃用,并建议使用WorkManagerJobIntentService ,并声明有必要从Z035489FF8D092741943E4A8324 迁移到 WorkManager。 I see two ways to solve this problem:我看到了两种解决这个问题的方法:

  1. Start OneTimeWorkRequest and specify to restart this method periodically in this method while the application is running in the background.启动OneTimeWorkRequest并指定在应用程序在后台运行时在此方法中定期重新启动此方法。
  2. Run PeriodicWorkRequest with a minimum allowed interval of 15 minutes.以 15 分钟的最小允许间隔运行PeriodicWorkRequest In this method, run the JobIntentService method, which runs for up to about 10 minutes , but the method may not run or may be destroyed by the system before it is complete.在该方法中,运行 JobIntentService 方法,该方法最多运行 10 分钟左右,但该方法可能无法运行,也可能在完成之前被系统破坏。

I'm worried about:我对某事感到担心:

  • potential memory leaks;潜在的 memory 泄漏;
  • potential problems with WorkManager or JobIntentService when going from foreground to background and vice versa从前台到后台时,WorkManager 或 JobIntentService 的潜在问题,反之亦然
  • the ability to use the MVVM pattern使用 MVVM 模式的能力

I would prefer going with second option, it gives you more time between each rescheduling of the Worker .我更喜欢使用第二个选项,它可以让您在每次重新安排Worker之间有更多时间。

Regarding your concerns:关于您的担忧:

  • Only possible leak is misconducting location callbacks.唯一可能的泄漏是不当的位置回调。 This can be easily tracked down, you shouldn't worry too much about it.这可以很容易地追踪到,你不应该太担心它。
  • Scheduled 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 ,并清除任何对用户可见的计划或正在进行的工作。
  • I prefer to keep 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.

相关问题 Android在后台获取GPS位置 - Android get GPS Location in the background 如何每隔30分钟在后台获取GPS位置 - How to get gps location every 30 mins in the background 如何使用 Target API 23 及更高版本通过 GPS 获取用户位置 - How to get users Location through GPS with Target API 23 and higher 考虑到电池,Android在后台重复获取位置的最佳方式 - Android best way to get location repeatedly in background considering battery as well 有没有办法在 API30 或更高版本的 Android 电视上显示通知? - Is there any way to show notification on an Android TV for API30 or higher? 融合位置API-在后台平衡,但有时会获取GPS位置 - Fused Location API - Balanced in background but sometimes get GPS location android获取GPS位置 - android get GPS location Android Xamarin 后台服务 - 每 x 分钟获取一次 GPS 位置? - Android Xamarin Background service - Get GPS Location every x minutes? 如何在Android的AsyncTask后台线程中获取当前的GPS位置? - How to get current GPS location in AsyncTask background Thread in android? 如何在 Android SDK API 29 级或更高级别中获取 IMSI 编号? - How to get IMSI Number in Android SDK API Level 29 or higher?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM