如何提供啟用 GPS 的選項 (How to give option for enable GPS)


問題描述

如何提供啟用 GPS 的選項 (How to give option for enable GPS)

我想在禁用 GPS 時顯示打開 GPS 的對話框。

在此處輸入圖片描述

查看圖片。當我點擊“是”時,應該會自動打開我的 GPS 而無需進入設置。

那麼,我如何通過選擇“是”選項來啟用 GPS 呢?


參考解法

方法 1:

This is part of Google play service api since 7.0 called SettingsApi.

Location settings ‑ While the FusedLocationProviderApi combines multiple sensors to give you the optimal location, the accuracy of the location your app receives still depends greatly on the settings enabled on the device (GPS, wifi, airplane mode, and others). Using the new SettingsApi class, you can bring up a Location Settings dialog which displays a one‑touch control for users to change their settings without leaving your app.

To use this API, first create a GoogleApiClient which supports at least LocationServices.API. Then connect the client to Google Play services:

mGoogleApiClient = new GoogleApiClient.Builder(context)
     .addApi(LocationServices.API)
     .addConnectionCallbacks(this)
     .addOnConnectionFailedListener(this)
     .build()
 ...
 mGoogleApiClient.connect();

Then create a LocationSettingsRequest.Builder and add all of the LocationRequests that the app will be using:

LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
     .addLocationRequest(mLocationRequestHighAccuracy)
     .addLocationRequest(mLocationRequestBalancedPowerAccuracy);

For next steps please check here: https://developers.google.com/android/reference/com/google/android/gms/location/SettingsApi

IMPORTANT: If the status code is RESOLUTION_REQUIRED, the client can call startResolutionForResult(Activity, int) to bring up a dialog, asking for user's permission to modify the location settings to satisfy those requests. The result of the dialog will be returned via onActivityResult(int, int, Intent). If the client is interested in which location providers are available, it can retrieve a LocationSettingsStates from the Intent by calling fromIntent(Intent)

Also please refer to this official repo: https://github.com/googlemaps/android‑samples/tree/master/ApiDemos if you want to grant the permission on API 23(Android M) at the running time.

(by user3997016bjiang)

參考文件

  1. How to give option for enable GPS (CC BY‑SA 2.5/3.0/4.0)

#google-play-services #gps #Android #android-location






相關問題

無法從 Android SDK 管理器安裝/下載 SDK (Unable to install/download SDK from Android SDK manager)

google-play-services_lib.jar bị thiếu trong thư mục lib SDK google_play_services (google-play-services_lib.jar missing from SDK google_play_services lib folder)

谷歌播放服務認證(oauth 2.0) (Google play service authentication (oauth 2.0))

在帶有 Android 的 Xamarin Studio 上使用 AdMob,Google Play 服務不存在 (Use AdMob on Xamarin Studio with Android, Google Play Services don't exist)

谷歌登錄使用新的 GoogleSignInOptions 獲取訪問令牌 (Google login get access token with new GoogleSignInOptions)

在組織內分發 Android 應用 (Distribute Android apps within organization)

如何提供啟用 GPS 的選項 (How to give option for enable GPS)

玩遊戲服務,我們能知道我們什麼時候通過另一個hiscore嗎? (Play Game Services, can we know when we pass another hiscore?)

Android 使用 Google 的位置服務對話框 (Android Use Google's location service dialog)

statusCode=SIGN_IN_REQUIRED, resolution=null 只有開發者 ID 可以登錄 else 錯誤 (statusCode=SIGN_IN_REQUIRED, resolution=null Only developer Id can Login else error)

項目中同時擁有 GMS 和 HMS (Have both GMS and HMS in the project)

谷歌cast MediaNotificationService 中的大量RemoteServiceExceptions (Large number of RemoteServiceExceptions in Google's cast MediaNotificationService)







留言討論