后台位置信息使用最佳实践
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本页介绍了请求和管理后台位置信息使用权限的最佳实践。
请求“始终允许”位置信息权限
从 Android 14 开始,应用必须具有 ACCESS_BACKGROUND_LOCATION
权限才能访问用户的位置信息。Navigation SDK 在其清单文件中包含此权限,因此您无需明确请求此权限(如果不需要用于其他用途),因为 Gradle 清单合并器会确保此权限与应用的清单合并。
不过,仅拥有 ACCESS_BACKGROUND_LOCATION
权限不足以在后台访问位置信息,我们建议您向用户请求“始终允许”位置信息权限。这样可确保应用在后台运行并显示通知,从而在导航期间最大限度地提高位置信息精确度。
提示应向用户说明,授予该权限后,应用在后台运行时,位置信息精确度会提高,导航体验也会得到改善。
如需详细了解如何提示用户授予位置信息访问权限,请参阅 Android 开发者文档中的请求位置信息权限 | 传感器和位置信息 | Android 开发者。
确保正确清理导航通知
为确保应用不会在不再需要通知后仍保持通知处于有效状态,请务必执行以下清理步骤:
- 调用
startGuidance()
后,请务必调用 stopGuidance()
或 clearDestination()
。
- 注册
ArrivalListener
后,请务必将其取消注册。
- 注册
RoadSnappedLocationProvider
后,请务必将其取消注册。
完成这些步骤可确保在不再需要通知时不会让通知保持活动状态,从而避免电池电量耗尽和可能出现的内存泄漏。
重新调用 NavigationApi#initForegroundServiceManager 方法时,请先调用 NavigationApi#clearForegroundServiceManager
如果您的应用使用的是 ForegroundServiceManager
,请在调用 NavigationApi#initForegroundServiceManagerProvider
之前调用 NavigationApi#clearForegroundServiceManager
;如果您已初始化 ForegroundServiceManager
,请调用 NavigationApi#initForegroundServiceManagerMessageAndIntent
。您可以在初始化前台服务管理器后执行此操作,以更新通知 ID 或通知内容。
Android 14 中引入的新限制简介
Android 14(Android U)针对应用在后台访问用户位置信息引入了新的限制。为了帮助缓解这些变化,Navigation SDK 在版本 5.4.0 中进行了更新,以更好地管理后台位置信息访问权限。我们还建议您更新实现,以确保应用继续能够访问最精确的位置数据。
Android 14 中的变更对 Navigation SDK 的影响
当您在应用中调用 startGuidance()
时,前台服务会开始显示逐向导航的用户通知。精细导航功能需要能够访问用户的位置信息,以便更新路线并显示正确的视觉和音频引导。从 Android 14 开始,在后台访问用户的精确位置信息需要获得用户授予的权限。默认情况下,如果应用未获得用户授予的后台位置信息访问权限,并尝试启动前台服务以获取位置信息更新,系统会抛出 SecurityException
,导致应用崩溃。
Navigation SDK 如何缓解此问题
从版本 5.4.0 开始,Navigation SDK 会处理此 SecurityException
,而不会影响应用,从而允许导航在后台继续运行。此外,Navigation SDK 还会在其 AndroidManifest
中包含 ACCESS_BACKGROUND_LOCATION
权限。这样一来,您的应用就不需要自行声明该权限,因为 Gradle 合并会处理该权限。不过,如果应用在进入后台之前未向用户显示通知,导航 SDK 将依赖系统来提供位置更新。这些系统更新可能不够频繁或不够精确,从而导致导航体验欠佳。因此,我们建议您也提示用户授予后台位置信息访问权限。
应用开发者可以采取哪些措施来提高导航的定位精确度
您可以更新应用,提示用户授予后台位置信息访问权限,从而提高 Navigation SDK 所用位置信息信号的精确度。如需了解详情,请参阅请求“始终允许”位置信息权限。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThis page outlines best practices for requesting and managing background location permissions in Android apps using the Navigation SDK.\u003c/p\u003e\n"],["\u003cp\u003eTo ensure optimal navigation, request "Allow all the time" location permission to maximize accuracy, especially when the app is in the background.\u003c/p\u003e\n"],["\u003cp\u003eProperly manage navigation notifications and foreground services to prevent battery drain and potential memory leaks.\u003c/p\u003e\n"],["\u003cp\u003eAndroid 14 introduces new location restrictions; the Navigation SDK is updated to mitigate these but requesting background location permission is still recommended.\u003c/p\u003e\n"],["\u003cp\u003eBy following these practices, developers can improve location accuracy and provide a seamless navigation experience for users.\u003c/p\u003e\n"]]],[],null,["# Background location usage best practices\n\nThis page explains best practices for requesting and managing background\nlocation usage permissions.\n\nRequest \"Allow all the time\" location permissions\n-------------------------------------------------\n\n[Starting with Android 14](#android-14-changes), apps must have the\n`ACCESS_BACKGROUND_LOCATION` permission in order to access the user's location.\nThe Navigation SDK includes this permission in its manifest file, so you don't\nneed to explicitly request it (if not needed for other purposes), since the\nGradle manifest merger will ensure it is merged with the app's manifest.\n\nHowever, having the `ACCESS_BACKGROUND_LOCATION` permission is not enough to\naccess location in the background and we recommend that you request \"Allow all\nthe time\" location permissions from users. This ensures that the app can run in\nthe background and show notifications, which maximizes location accuracy during\nnavigation.\n\nThe prompt should explain to users how granting the permission will improve\nlocation accuracy and improve their navigation experience when the app is\nrunning in the background.\n\nFor more information on prompting users for location access, see [Request\nlocation permissions \\| Sensors and location \\| Android\nDevelopers](https://developer.android.com/develop/sensors-and-location/location/permissions#background-dialog-target-sdk-version)\nin the Android developer documentation.\n\nEnsure proper cleanup of navigation notifications\n-------------------------------------------------\n\nTo ensure that your app doesn't keep notifications alive after they're no longer\nneeded, make sure you perform the following cleanup steps:\n\n- After invoking `startGuidance()`, be sure to invoke either `stopGuidance()` or `clearDestination()`.\n- After registering `ArrivalListener`, make sure to unregister it.\n- After registering `RoadSnappedLocationProvider`, make sure to unregister it.\n\nCompleting these steps ensures that notifications aren't kept alive when they're\nno longer needed, which could lead to battery drains and possible memory leaks.\n\nWhen re-invoking NavigationApi#initForegroundServiceManager methods, invoke NavigationApi#clearForegroundServiceManager first\n-----------------------------------------------------------------------------------------------------------------------------\n\nIf your app is using the `ForegroundServiceManager`, invoke\n`NavigationApi#clearForegroundServiceManager` before invoking\n`NavigationApi#initForegroundServiceManagerProvider`, and if you have already\ninitialized the `ForegroundServiceManager`, invoke\n`NavigationApi#initForegroundServiceManagerMessageAndIntent`. You can do this to\nupdate the notification ID or the notification content after you have\ninitialized the foreground service manager.\n\nAbout the new restrictions introduced in Android 14\n---------------------------------------------------\n\nAndroid 14 (Android U) [introduced new\nrestrictions](https://developer.android.com/develop/background-work/services/foreground-services#wiu-restrictions)\non apps accessing the user's location in the background. To help mitigate these\nchanges, the Navigation SDK was updated in version 5.4.0 to better manage\nbackground location access. We also recommend that you update your\nimplementation to ensure that your app continues to have access to the most\nprecise location data.\n\n### How changes in Android 14 impact the Navigation SDK\n\nWhen you invoke\n[`startGuidance()`](/maps/documentation/navigation/android-sdk/reference/com/google/android/libraries/navigation/Navigator#public-abstract-void-startguidance)\nin your app, the foreground service starts showing user notifications for\nturn-by-turn navigation. Turn-by-turn navigation is dependent on being able to\naccess the user's location in order to update the route and show the correct\nvisual and audio guidance. Starting in Android 14, accessing the user's precise\nlocation in the background requires permission from the user. By default, if an\napp does not have the user's permission for background location access and\nattempts to start a foreground service for location updates, the system throws a\n`SecurityException`, causing the app to crash.\n\n### How the Navigation SDK mitigates this issue\n\nStarting with version 5.4.0, the Navigation SDK handles this `SecurityException`\nwithout impacting the app, allowing navigation to continue running in the\nbackground. Additionally, the Navigation SDK includes the\n`ACCESS_BACKGROUND_LOCATION` permission in its `AndroidManifest`. This way, your\napp doesn't need to declare the permission itself, as Gradle merging will handle\nit. However, if a notification is not displayed to the user before the app goes\ninto the background, the Navigation SDK will rely on the system to provide\nlocation updates. These system updates may not be frequent or precise and result\nin suboptimal navigation experience. For this reason, we recommend that you also\nprompt users for background location access.\n\n### What app developers can do to improve location accuracy for navigation\n\nYou can improve the accuracy of the location signal used by the Navigation SDK\nby updating your app to prompt users for background location access. For more\ninformation, see [Request \"Allow all the time\" location\npermissions](#request-allow-all-the-time)."]]