1

If I open the Google Maps app with GPS switched off, and I try to get my position, I get the prompt in figure 1 (sorry if figures are in italian).

I found several answers in StackOverflow, but all of them suggest to do something like this:

if(gpsNotEnabled) {
    AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    dialog.setMessage("GPS not enabled");
    dialog.setPositiveButton("Enable", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface paramDialogInterface, int paramInt) {
            Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(myIntent);
            //get gps coordinates
        }
    });
    dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface paramDialogInterface, int paramInt) {
            // Do something without gps
        }
    });
    dialog.show();
}

But if I do this, I get a dialog like in figure 2; and if I click enable, I get to the GPS settings, like in picture 3, where I can switch on GPS and then get back.

enter image description here

This is not the behaviour we can see in Google Map: if I click yes in the prompt, the GPS is automatically switched on without even the need to exit the current app. How can we achieve this behaviour?

ocramot
  • 1,361
  • 28
  • 55
  • https://stackoverflow.com/questions/29697725/enable-gps-programatically-android-without-navigating-to-the-location-settings/29697889?s=4|0.9015#29697889 – CommonsWare Sep 02 '16 at 15:15
  • Oh good, couldn't find that by myself, maybe I searched wrong. Thanks! – ocramot Sep 02 '16 at 15:58

0 Answers0