Pages

Ads 468x60px

Monday, August 19, 2013

Android: Show Alert Dialog to enable GPS



This code will show alert dialog to enable GPS on the device.


// Show Alert Dialog to enable GPS
protected void showGPSAlert(final Context context) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder
.setMessage(
"GPS is disabled on your device. Would you like to enable it?")
.setCancelable(false)
.setPositiveButton("Open Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// set intent to open settings
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(callGPSSettingIntent);
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}

No comments:

Post a Comment