Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable GPS programmatically Android (without navigating to the location settings)

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)?

like image 288
Abhilash Avatar asked Apr 17 '15 11:04

Abhilash


People also ask

How do I turn on GPS automatically on Android?

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.

How enable and disable GPS programmatically in Android?

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.

How do I open location settings in android programmatically?

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.


2 Answers

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.

like image 126
CommonsWare Avatar answered Sep 20 '22 22:09

CommonsWare


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);
like image 37
Laurenswuyts Avatar answered Sep 19 '22 22:09

Laurenswuyts