Created
August 13, 2016 12:15
-
-
Save Tourenathan-G5organisation/abeb052562c96713c5f2b0878d5e35ec to your computer and use it in GitHub Desktop.
Check GPS activation on Android and prompt user to change GPS setting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE ); | |
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) { | |
buildAlertMessageNoGps(); | |
} | |
private void buildAlertMessageNoGps() { | |
final AlertDialog.Builder builder = new AlertDialog.Builder(this); | |
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?") | |
.setCancelable(false) | |
.setPositiveButton("Yes", new DialogInterface.OnClickListener() { | |
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) { | |
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); | |
} | |
}) | |
.setNegativeButton("No", new DialogInterface.OnClickListener() { | |
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) { | |
dialog.cancel(); | |
} | |
}); | |
final AlertDialog alert = builder.create(); | |
alert.show(); | |
} | |
// I would advise declaring alert for the whole activity so you can dismiss it here in onDestroy to avoid a memory leak | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
if(alert != null) { alert.dismiss(); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment