I'm creating a simple app in which I'm displaying a few markers on the map. I'm using the following code to display an AlertDialog
if the Location is off
:
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
//Set title, message and other stuff.
try {
if (Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE) == Settings.Secure.LOCATION_MODE_OFF) {
alertDialog.show(); //Works perfect!
}
} catch (Settings.SettingNotFoundException e) {e.printStackTrace();}
//Create a listener to check for Location Settings Changes
LocationListener locationListener = new LocationListener() {
public void onProviderEnabled(String provider) {alertDialog.dismiss();} //Dismiss the AlertDialog
// The other overloaded methods: onProviderDisabled, onLocationChanged and onStatusChanged
};
When I start the app for the first time with the location turned off
, the AlertDialog
appears. If I turn on
the Location like in the picture below:
The onProviderEnabled()
method is not triggered. If I close the restart the app, everything works fine. And here is how I request location updates.
if (isGooglePlayServicesOk()) {
String[] permissions = {COARSE_LOCATION, FINE_LOCATION};
if (COARSE LOCATION PERMISSION GRANTED CONDITION) {
if (FINE LOCATION PERMISSION GRANTED CONDITION) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
}
}
Please help me dismiss the AlertDialog
from the first time the app starts. Thanks!!
Method is not called because app has already run past it and it found that gps was off, so it did not dismiss the dialog. You can either start some thread for like 10 seconds to check again or try to listen to change in location providers. For that check out this answer.