16

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
LEE
  • 3,335
  • 8
  • 40
  • 70
  • 1
    Please refer this link.. http://stackoverflow.com/questions/33251373/turn-on-location-services-without-navigating-to-settings-page –  Sep 28 '16 at 11:43

2 Answers2

13

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Here is the documentation of the SettingsApi: https://developers.google.com/android/reference/com/google/android/gms/location/SettingsApi – Daan Jul 22 '15 at 19:36
5

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);
avalancha
  • 1,457
  • 1
  • 22
  • 41
Laurenswuyts
  • 2,144
  • 4
  • 21
  • 39
  • Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE What am i missing? i've already added the permissions. – LEE Apr 17 '15 at 12:29
  • They didn't embed the settings page into the popup. Absolutely not. – Joaquin Iurchuk Jul 20 '15 at 13:00
  • how do you get the user back to your app after redirecting them to the ugly settings screen?.. Google Maps' way is very intuitive - but I guess that's not an out-of-the-box component? – Don Cheadle Aug 23 '15 at 00:33
  • 4
    This doesn't answer the question. This is the tradition way of approaching this problem, the creator is looking for the newest way. – Michael Sep 22 '15 at 17:17
  • You can found the answer here http://stackoverflow.com/questions/29744257/android-can-i-enable-the-gps-without-redirecting-the-user-to-the-settings-scree – Gorio Feb 01 '16 at 18:28