כפתור להרשאת מיקום חד פעמית ב-Android הוא רכיב בממשק המשתמש של המערכת שאפשר להתאים אישית, והוא נועד לפשט את הדרך לבקשת גישה למיקום מדויק ברמת הסשן. הכפתור משפר את פרטיות המשתמשים ומצמצם את החיכוך שנוצר כתוצאה מתיבות דו-שיח חוזרות ונשנות לבקשת הרשאה, שמופיעות בדרך כלל כשמעניקים הרשאה זמנית מסוג 'רק הפעם'. זאת משום שהכפתור מאפשר לשלוח בקשות למיקום באמצעות פעולה ישירה של המשתמש.
אם האפליקציה שלכם מיועדת ל-Android 17 (רמת API 37) או לגרסאות מאוחרות יותר, והיא מכילה רק תכונות שדורשות גישה למיקום על בסיס סשן כדי לפעול, המדיניות של Google Play מחייבת אתכם להשתמש בכפתור להרשאת מיקום חד פעמית. פרטים נוספים זמינים במדיניות בנושא כפתור להרשאת מיקום חד פעמית.
מתי כדאי להשתמש בכפתור להרשאת מיקום חד פעמית
משתמשים בכפתור להרשאת מיקום חד פעמית לתכונות שדורשות גישה מדויקת למיקום לזמן קצר או לזמן השימוש באפליקציה. האפשרות הזו מתאימה לאפליקציות שלא דורשות גישה מתמשכת למיקום, ומטרתה לצמצם את ההודעות החוזרות לבקשת הרשאה עם האפשרות 'רק הפעם'.
תרחישים נפוצים לדוגמה:
- פונקציות של חיפוש בסביבה: חיפוש של מלונות, חנויות או מסעדות בקרבת מקום.
- שיתוף מיקום: שיתוף המיקום הנוכחי שלכם עם חברים או בני משפחה.
- רשתות חברתיות: צ'ק-אין או תיוג מיקום.
- מסחר אלקטרוני: מילוי אוטומטי של כתובת למשלוחים.
התאמה אישית של ממשק המשתמש
כדי לוודא שהלחצן תואם למראה האפליקציה ועדיין יהיה מזוהה, אפשר לשנות את הרכיבים החזותיים הבאים:
- ערכת צבעים לרקע ולסמלים.
- סגנון, גודל וצורה של קו המתאר.
- תוויות טקסט מרשימה מוגדרת מראש (לדוגמה, 'שימוש במיקום מדויק', 'שיתוף מיקום מדויק').
הטמעה של כפתור להרשאת מיקום חד פעמית
כדי לשלב את כפתור להרשאת מיקום חד פעמית, משתמשים בספריית 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 composable
הדוגמה הבאה היא הטמעה של כפתור להרשאת מיקום חד פעמית, כולל דוגמאות לשימוש באפשרויות ההתאמה האישית הזמינות, שאפשר להשתמש בהן כדי להתאים את ממשק המשתמש לשאר האפליקציה.
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 או גרסאות קודמות.