I am checking in GPS status (is it active or passive) on My application,When Gps is closed, Alert Prompt Dialog opens and it asked "for a better experience turn on device location which uses google's location service" and When I click No Thanks or Ok it doesn't fall inside of onActivityResult method,how can I handle result of alert prompt dialog? I shared GpsUtils class at below
public class GpsUtils extends AppCompatActivity {
private Context context;
private SettingsClient mSettingsClient;
private LocationSettingsRequest mLocationSettingsRequest;
private LocationManager locationManager;
private LocationRequest locationRequest;
public GpsUtils(Context context) {
this.context = context;
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
mSettingsClient = LocationServices.getSettingsClient(context);
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(10 * 1000);
locationRequest.setFastestInterval(2 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
mLocationSettingsRequest = builder.build();
//**************************
builder.setAlwaysShow(true); //this is the key ingredient
//**************************
}
// method for turn on GPS
public void turnGPSOn(final onGpsListener onGpsListener) {
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
if (onGpsListener != null) {
onGpsListener.gpsStatus(true);
}
} else {
mSettingsClient
.checkLocationSettings(mLocationSettingsRequest)
.addOnSuccessListener((Activity) context, new OnSuccessListener<LocationSettingsResponse>() {
@SuppressLint("MissingPermission")
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
// GPS is already enable, callback GPS status through listener
if (onGpsListener != null) {
onGpsListener.gpsStatus(true);
}
}
})
.addOnFailureListener((Activity) context, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
int statusCode = ((ApiException) e).getStatusCode();
switch (statusCode) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
// Show the dialog by calling startResolutionForResult(), and check the
// result in onActivityResult().
ResolvableApiException rae = (ResolvableApiException) e;
rae.startResolutionForResult((Activity) context, AppConstants.GPS_REQUEST);
} catch (IntentSender.SendIntentException sie) {
Log.i(TAG, "PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
String errorMessage = "Location settings are inadequate, and cannot be " +
"fixed here. Fix in Settings.";
Log.e(TAG, errorMessage);
Toast.makeText((Activity) context, errorMessage, Toast.LENGTH_LONG).show();
}
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == AppConstants.GPS_REQUEST) {
isGPS = true; // flag maintain before get location
getLocation();
// isContinue = true;
stringBuilder = new StringBuilder();
}
}else{
Main.this.finish();
System.exit(0);
}
}
public interface onGpsListener {
void gpsStatus(boolean isGPSEnable);
}
}