Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

android - How to give option for enable GPS

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

enter image description here

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...