How to give option for enable GPS

醉酒当歌 提交于 2019-12-12 06:49:25

问题


I want to show dialog box for turn on GPS when GPS is disable .

See the image. When i click on YES then should be automatically turn on my GPS without going into setting.

So , how can i enable GPS by select YES option ?


回答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.



来源:https://stackoverflow.com/questions/33969085/how-to-give-option-for-enable-gps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!