Plain and simple. I start an Activity and check if the phone has the GPS module enabled or not. If it isn't enabled, I prompt the user with a dialog and ask if he wants to manually enable it. On Yes I activate the Settings for Location. Now the user can enable it if he wants to, but I need to check what he has done.
try {
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {}
if (isGPSEnabled) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(
"Your GPS module is disabled. Would you like to enable it ?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// Sent user to GPS settings screen
final ComponentName toLaunch = new ComponentName(
"com.android.settings",
"com.android.settings.SecuritySettings");
final Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(toLaunch);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, 1);
dialog.dismiss();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
I need to know what the user has chosen in Location settings, when he comes back in order to proceed with my logic in code. Basically I need to wait for the user to make a choice and return to my activity, and also to recheck the status of gps module again.
Why not just check LocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
in your onActivityResult()
method? When the user returns, this method should be called because you called startActivityForResult()
. If the user has enabled GPS then isProviderEnabled()
should now return a different result.
Alternatively, always do the GPS check in your onResume method. If the user returns from the Location settings and hasn't enabled GPS, then they will simply receive the same prompt until GPS is enabled.
Or am I missing something?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With