Android 位置資訊按鈕是可自訂的系統 UI 元素,可簡化您要求以工作階段為範圍的精確位置存取權的方式。透過使用者直接操作發起位置資訊要求,這個按鈕可提升使用者隱私權,並減少重複權限對話方塊造成的阻礙,這類對話方塊通常會在授予「僅限這次」的臨時權限時出現。
如果應用程式指定 Android 17 (API 級別 37) 以上版本,且僅包含需要工作階段位置存取權才能運作的功能,則根據 Google Play 政策規定,您必須使用位置資訊按鈕。詳情請參閱位置資訊按鈕政策。

何時使用位置資訊按鈕
如果功能需要暫時存取精確位置資訊 (以工作階段為準),請使用位置資訊按鈕。如果應用程式不需要持續存取位置資訊,且目標是減少重複顯示「僅限這次」權限提示,就很適合使用這項功能。
常見用途包括:
- 「搜尋附近」功能:尋找附近的飯店、商店或餐廳。
- 分享位置資訊:與親友分享目前的所在位置。
- 社群媒體:簽到或位置標記。
- 電子商務:自動填入外送地址。
自訂 UI
為確保按鈕符合應用程式的美學風格,同時仍可辨識,您可以修改下列視覺元素:
- 背景和圖示色彩配置。
- 外框樣式、大小和形狀。
- 預先定義清單中的文字標籤 (例如「使用精確位置」、「分享精確位置」)。
實作位置資訊按鈕
如要整合位置資訊按鈕,請使用 Jetpack 程式庫。這個程式庫可簡化設定、處理較新平台上的安全算繪作業,並為以 Android 16 以下版本為目標的應用程式提供備援機制。
步驟 1:在 Android 資訊清單中宣告權限
您必須一併聲明標準位置存取權,以及系統遠端算繪服務所需的專屬 USE_LOCATION_BUTTON 權限。
<?xml version="1.0" encoding="utf-8"?> <!-- Copyright 2026 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 1. Standard Coarse and Fine Location Permissions --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- Optional: If your app is only using the location button to access location, you should add the "onlyForLocationButton" flag shown below to your ACCESS_FINE_LOCATION declaration. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:usesPermissionFlags="onlyForLocationButton"/> Note: Adding this flag restricts your app from accessing the precise location permission via the broader permission, and that users will be required to use the location button in order to share precise location with the app. This is designed to improve user privacy & trust when granting location access. --> <!-- 2. CRITICAL: Required system permission for rendering the LocationButton --> <uses-permission android:name="android.permission.USE_LOCATION_BUTTON" /> <application android:icon="@mipmap/ic_launcher" android:label="LocationButtonSample" android:theme="@style/Theme.PinPoint"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
步驟 2:實作 Kotlin 可組合函式
以下是位置資訊按鈕的實作範例,包括使用可用自訂選項的範例,可用於讓 UI 與應用程式的其餘部分相符。
import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.width import androidx.compose.runtime.Composable import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.dp import androidx.core.locationbutton.compose.LocationButton import androidx.core.locationbutton.compose.LocationButtonTextType @Composable fun LocationPermissionScreen(onPermissionGranted: () -> Unit, onPermissionDenied: () -> Unit) { // Renders the secure system-trusted Location Button composable LocationButton( // Callback triggered when the user taps the secure button and makes a decision on the permission dialog onPermissionResult = { isGranted -> if (isGranted) { onPermissionGranted() } else { onPermissionDenied() } }, /* ============================================================================ * VISUAL CUSTOMIZATIONS * Un-comment any of the parameters below to customize the button's aesthetics. * If omitted, the button falls back to secure, high-contrast system defaults. * ============================================================================ */ /* // LABEL TEXT TYPE: // Predefined system strings rendered inside the secure process. // Options: PreciseLocation, UsePreciseLocation, SharePreciseLocation, // NearMyPreciseLocation, or None (for an icon-only button). textType = LocationButtonTextType.UsePreciseLocation, // COLOR PALETTE: // Customize the container background, text label, and icon tint colors. backgroundColor = Color(0xFF00796B), // e.g., Material Teal textColor = Color.White, iconTint = Color(0xFFFFC107), // e.g., Amber icon tint // CORNER RADIUS & SHAPE: // Define the resting corner radius and the morphed radius when pressed. cornerRadius = 24.dp, // Rounded capsule shape pressedCornerRadius = 12.dp, // Morphs to sharper corners on tap // OUTLINE STROKE (BORDERS): // Add a contrasting outline stroke around the button bounds. strokeColor = Color(0xFF004D40), strokeWidth = 2.dp, // INTERACTIVE TOUCH PADDING: // Defines the secure clickable touch target boundary. // Coerced securely by the system between 4.dp and 8.dp. clickablePadding = PaddingValues(6.dp) */ ) }
步驟 3:處理回溯相容性
Jetpack 程式庫會自動處理舊版 Android 的回溯相容性。在搭載 Android 16 以下版本的裝置上,程式庫會改用本機算繪的元件,保留自訂的視覺版面配置,但會還原為觸發標準位置存取權提示。
採用這種做法,您就能享有採用位置資訊按鈕的優點,不必為搭載 Android 16 以下版本的裝置維護平行解決方案。