自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
三態位置存取權
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android 10 中的三態位置存取權可讓使用者進一步控管應用程式存取裝置位置資訊的方式。
在 Android 9 以下版本中,使用者在授予應用程式位置存取權時,會做出永久性的選擇。您可以選擇「拒絕」或「允許」,後者會讓應用程式一律取得存取權 (前景和背景)。Android 10 中的三態位置資訊存取權可讓使用者選擇三種選項,允許應用程式存取裝置的位置資訊。應用程式要求權限時,系統會提示使用者授予或拒絕該權限層級。
使用者通常會看到圖 1 所示的三個選項。不過,在某些用途中,您只需要這兩種選項,在這種情況下,系統只會顯示這兩種選項。
圖 1. 三態通知畫面。
三個選項如下:
- 「一律允許」:即使您未使用應用程式 (在背景執行),應用程式仍可取得裝置的位置資訊。這相當於在 Android 9 以下版本中允許權限。
- 僅在使用該應用程式時允許:(僅限前景) 只有在應用程式正在執行時,應用程式才能看到裝置的位置資訊。
- 拒絕:應用程式永遠無法存取裝置的位置資訊。這與在 Android 9 以下版本中拒絕權限相同。
應用程式要求位置存取權時,系統會提示使用者授予權限。
使用者授予「僅在使用該應用程式時允許」存取權後,應用程式可以要求逐漸提升存取權,直到「隨時允許」為止。使用者會看到要求對話方塊 (如圖 2 所示)。如果使用者選取「保留使用期間的存取權」,應用程式下次使用時存取裝置位置時,對話方塊會提供「保留並不再詢問」選項。
針對指定 Android 10 為目標版本的應用程式,對話方塊會在下列情況下顯示:
- 授予權限後至少 24 小時。
- 只有在應用程式在背景接收位置資訊時才會發生。
- 螢幕開啟且使用者未使用其他應用程式時。
圖 2. 增量權限。
如要進一步瞭解如何要求權限,請參閱「應用程式存取裝置位置資訊」。如果您的應用程式指定 Android 9 以下版本為目標,請參閱「
繼續執行使用者啟動的動作」一文。
影響
三態位置資訊存取權功能會影響所有需要在背景執行時存取裝置位置資訊的應用程式,且在 Android 10 中為必備。
您可以變更程式碼,但不得變更或自訂架構中的權限相關行為。
實作
三態位置資訊權限會套用至 Android 10 中的應用程式,無論應用程式的目標 SDK 為何。
如要瞭解如何在升級時實作應用程式的用途,請參閱開發人員說明文件中的「
為裝置升級情境進行設計」一節。
如要瞭解如何為不同用途啟用存取權 (例如,為 Google 地圖或 Google Play 服務等應用程式啟用背景位置資訊存取權),請參閱「應用程式存取裝置位置資訊」頁面中的以下主題:
應用程式內的位置資訊存取權
使用者可以選擇將應用程式的存取權限變更為「拒絕」或「僅在使用該應用程式時允許」。針對應用程式內的位置存取權,以及所有第一方和第三方應用程式,請提供下表所列的使用者控管層級。
應用程式需要要求的權限類型 |
提供的使用者選項 |
應用程式僅要求前景權限 |
僅在使用該應用程式時允許
拒絕
|
應用程式一律要求權限 (前景和背景)
|
一律允許
僅在使用該應用程式時允許
拒絕
|
所有提出位置存取權要求的應用程式
|
僅在使用應用程式時允許 |
這些權限適用於所有位置要求。具有「僅在使用該應用程式時允許」權限的應用程式,無法在背景執行 Wi-Fi 或行動網路掃描。
在 Android 11 以下版本中,如果應用程式具有「僅在使用該應用程式時允許」權限,則無法進行背景藍牙掃描。自 Android 12 起,具有「僅在使用該應用程式時允許」權限的應用程式,只要將 android:usesPermissionFlags
屬性的值設為 neverForLocation
,即可取得藍牙掃描結果。詳情請參閱「應用程式未擷取實際位置資訊」。
作業系統升級
在升級至 Android 10 的作業系統上,應用程式位置存取權會根據下列方式轉譯:
- 「開啟」會變成「僅在使用時允許」。
- 保持「關閉」(「拒絕」)。
- 預先授予的位置存取權會變成「僅在使用時允許」預先授權。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","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-07-27 (世界標準時間)。"],[],[],null,["# Tristate location permissions in Android 10 give users more control over\nhow apps access their device locations.\n\nIn Android 9 and lower, users made persistent choices when granting location access to apps. They\ncould either **Deny** or **Allow**, the latter of which gave apps access\nall the time (foreground and background). Tristate location permissions in Android 10 give users three options for allowing an app access to a device's location.\nUsers are prompted to grant or deny the permission level when an app requests it.\n\nA user normally sees the three choices presented in Figure 1. However, there are use cases where\nonly two of these options are required, and in such cases only those two are shown.\n\n**Figure 1.** Tristate notifications screen.\n\nThese are the three options:\n\n- **Allow all the time**: the device's location is known to the app even when the app is not in use (running in the background). This is equivalent to allowing permission in Android 9 and lower.\n- **Allow only while the app is in use**: (foreground only) the device's location is only visible to the app when it's actively running.\n- **Deny**: the device's location is never visible to the app. This is the same as denying permission in Android 9 and lower.\n\nUsers are prompted to grant location access permission when apps request the\npermission.\n\nOnce a user grants **Allow only while the app is in use** access permission, an app\ncan request an incremental increase in access to **Allow all the time** . The user sees\na request dialog (shown in [Figure 2](#incremental-permissions-screens)). If the user\nselects **Keep while-in-use access** , when the app accesses device location on next\nuse, the dialog provides the option to **Keep and don't ask again**.\n\nThe dialog appears under these conditions for apps targeting Android 10:\n\n- After at least 24 hours of granting the permission.\n- Only if the app is receiving locations in the background.\n- When the screen is on, and the user is not utilizing another app.\n\n**Figure 2.** Incremental permissions.\n\nTo learn more about requesting permissions, see [App access to\ndevice location](https://developer.android.com/training/location/request-updates). For details if your app targets Android 9 and lower, see [Continuation of user-initiated action](https://developer.android.com/training/location/request-updates).\n\nImpact\n------\n\nThe tristate location permissions feature affects any app that needs device location access\nwhile running in the background, and is required in Android 10.\n\nYou may change your code but you *may not* alter or customize\npermission-related behavior in the framework.\n\nImplementation\n--------------\n\nTristate location permissions are applied to apps in Android 10\nirrespective of an app's target SDK.\n\nFor information on implementing your app's use cases (on upgrades), refer to the [Design for device upgrade scenarios](https://developer.android.com/training/location/request-updates) section in the developer documentation.\n\nTo see how to enable access for different use cases (such as requiring background location\naccess for apps like Google Maps or Google Play services), view these topics on the [App access to\ndevice location](https://developer.android.com/training/location/request-updates) page:\n\n- [Request background\n location](https://developer.android.com/training/location/request-updates)\n- [Periodic checks of\n a device's location](https://developer.android.com/training/location/request-updates)\n\n### In-app location access\n\nUsers can change your app's access permissions to either **Deny** or **Allow\nonly while using the app** if they choose. For in-app location access permission, and for all\nfirst-party and third-party apps, provide the levels of user control given in the following table.\n\n| Permission type app needs to request | User options to provide |\n|------------------------------------------------------------|--------------------------------------------------------|\n| App requests foreground permissions only | Allow only while using the app Deny |\n| App requests permission always (foreground and background) | Allow all the time Allow only while using the app Deny |\n| All apps with location access requests | Allow only while using the app |\n\nThese permissions apply to all location requests. Apps with **Allow\nonly while using the app** permissions aren't allowed background Wi-Fi or\ncell scans.\n\nOn Android 11 or lower, apps with **Allow\nonly while using the app** permissions aren't allowed background\nBluetooth scans. From Android 12, apps with\n**Allow only while using the app** permissions can gain Bluetooth\nscan results by setting the value of the\n`android:usesPermissionFlags` attribute to\n`neverForLocation`. For more details, see\n[App\ndoesn't derive physical location](https://developer.android.com/about/versions/12/features/bluetooth-permissions#not-derive-physical-location).\n\nOS upgrades\n-----------\n\nOn an OS upgrade to Android 10, app location permissions translate\naccording to the following:\n\n- **On** becomes **Allow only while in Use**.\n- **Off** remains off (**Deny**).\n- Pre-granted location access becomes the **Allow only while in use** pre-grant."]]