[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-09-04 UTC"],[[["\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)."]]