well changing these settings in code is not possible anymore after 4.1 (they may be a workarounds to turn it on because on a bug see here but it won't work on 4.3 or 4.4 or you need rooted phones, what you can do is show a dialog to the user to go to settings to enable the GPS
public static void LocationAlertDialog(final Context context) {
AlertDialog.Builder d = new AlertDialog.Builder(context);
d.setTitle("No Providers");
d.setMessage("Unfortunately, you don't have any providers to get your location, would you like to turn on your GPS?");
d.setNegativeButton(context.getResources().getString(R.string.dialogCancel), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
d.setPositiveButton(context.getResources().getString(R.string.dialogAccept), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivityForResult(i, 0);
}
});
d.show();
}
call the previous function in
if (!isGPSEnabled && !isNetworkEnabled) {
LocationAlertDialog(this);
Log.e(MyLogger.TAG, "Non providers are enabled");
then in your onActivityResult
you can check code 0 if the GPS has been turned on or not