Skip to content

Instantly share code, notes, and snippets.

@tauheed0007
Created June 30, 2022 04:09
Show Gist options
  • Save tauheed0007/6d80650b08e6db2a4227d3b54b1a167d to your computer and use it in GitHub Desktop.
Save tauheed0007/6d80650b08e6db2a4227d3b54b1a167d to your computer and use it in GitHub Desktop.
public void checkGPS() {
locationRequest = LocationRequest.create();
locationRequest.setInterval(1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest)
.setAlwaysShow(true);
Task<LocationSettingsResponse> locationSettingsResponseTask = LocationServices.getSettingsClient(getApplicationContext())
.checkLocationSettings(builder.build());
locationSettingsResponseTask.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@Override
public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
try {
LocationSettingsResponse response = task.getResult(ApiException.class);
Toast.makeText(MainActivity.this, "GPS is Already Enable", Toast.LENGTH_SHORT).show();
} catch (ApiException e) {
if (e.getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
ResolvableApiException resolvableApiException = (ResolvableApiException) e;
try {
resolvableApiException.startResolutionForResult(MainActivity.this, 101);
} catch (IntentSender.SendIntentException sendIntentException) {
sendIntentException.printStackTrace();
}
}
if (e.getStatusCode() == LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE) {
Toast.makeText(MainActivity.this, "Settings Not Available", Toast.LENGTH_SHORT).show();
}
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment