How to enable GPS programmatically, as it does happen in the official Google Maps app by just clicking on the 'turn on' option on the pop up screen (without navigating to location settings)?
Open your phone's Settings app. Under "Personal," tap Location access. At the top of the screen, turn Access to my location on or off.
This example demonstrates how do I get enable/disable GPS programmatically in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Get current location settingsTask<LocationSettingsResponse> task = client. checkLocationSettings(builder. build()); When the Task completes, your app can check the location settings by looking at the status code from the LocationSettingsResponse object.
The Google Maps app is using what is now available to us as SettingsApi
the Play Services SDK. You can use SettingsApi
to inquire as to whether your desired LocationRequest
can be fulfilled with whatever location providers are enabled. If it cannot be fulfilled, and Play Services thinks that the user can change this, you can ask for the dialog that you see Maps display pop up.
Using SettingsApi
is not especially simple. Here a sample app for that. Using ACTION_LOCATION_SOURCE_SETTINGS
, as suggested in Laurenswuyts' answer, is much simpler to implement.
That's because they are using the Settings API
in the play services as described in Commonsware's answer, which is a bit difficult. You are better off with the "old" method:
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With