Skip to content

Instantly share code, notes, and snippets.

@vladimirpetrovski
Created July 24, 2020 19:22
Show Gist options
  • Select an option

  • Save vladimirpetrovski/14b6c6160aa5f7042537bb69a81e772d to your computer and use it in GitHub Desktop.

Select an option

Save vladimirpetrovski/14b6c6160aa5f7042537bb69a81e772d to your computer and use it in GitHub Desktop.
fun checkLocationSettings(onSuccessCallback: () -> Unit) {
val locationRequest = LocationRequest.create()
locationRequest.priority = LocationRequest.PRIORITY_LOW_POWER
val builder = LocationSettingsRequest.Builder()
builder.addLocationRequest(locationRequest)
val client = LocationServices.getSettingsClient(this)
val task = client.checkLocationSettings(builder.build())
task.addOnSuccessListener(this) {
onSuccessCallback()
}
task.addOnFailureListener(this) {
if (it is ResolvableApiException) {
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
it.startResolutionForResult(this, 111)
} catch (sendEx: IntentSender.SendIntentException) {
// Ignore the error.
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment