i am making an app in android using java which show current address on map and give alert whenever gps is off.
public class LocationManagerCheck {
LocationManager locationManager;
Boolean locationServiceBoolean = false;
int providerType = 0;
static AlertDialog alert;
public LocationManagerCheck(Context context) {
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
boolean gpsIsEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean networkIsEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (networkIsEnabled == true && gpsIsEnabled == true) {
locationServiceBoolean = true;
providerType = 1;
} else if (networkIsEnabled != true && gpsIsEnabled == true) {
locationServiceBoolean = true;
providerType = 2;
} else if (networkIsEnabled == true && gpsIsEnabled != true) {
locationServiceBoolean = true;
providerType = 1;
}
}
public Boolean isLocationServiceAvailable() {
return locationServiceBoolean;
}
public int getProviderType() {
return providerType;
}
public void createLocationServiceError(final Activity activity) {
// show alert dialog if Internet is not connected
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(
"You need to activate location service to use this feature. Please turn on network or GPS mode in location settings")
.setTitle("LostyFound")
.setCancelable(false)
.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
activity.startActivity(intent);
dialog.dismiss();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
alert = builder.create();
alert.show();
}
I am using this code which only showing alert when Mapactivity
is starts but not in during the activity
.I just want, it's show me alert bo when gps is off or disable by user during MapActivity is open ,help me to find correct code thanks in advance.