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