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!!