Skip to content

Instantly share code, notes, and snippets.

@yusufceylan
Created September 7, 2020 12:02
Show Gist options
  • Save yusufceylan/0cb5e81dda9cdf2e807708e5aa4bdc2b to your computer and use it in GitHub Desktop.
Save yusufceylan/0cb5e81dda9cdf2e807708e5aa4bdc2b to your computer and use it in GitHub Desktop.
Huawei Location Kit - Check Device Location Settings
fun checkLocationSettingsAndShowPopup(
activity: Activity,
onSuccess: ((locationSettingsResponse: LocationSettingsResponse?) -> Unit)? = null
) {
val builder = LocationSettingsRequest.Builder()
val locationRequest = LocationRequest()
builder.addLocationRequest(locationRequest)
val locationSettingsRequest = builder.build()
mSettingsClient.checkLocationSettings(locationSettingsRequest)
.addOnSuccessListener {
// Initiate location requests when the location settings meet the requirements.
LogUtils.d("LocationManager -> Device location is open")
// Notify
onSuccess?.invoke(it)
}
.addOnFailureListener { e ->
// Device location settings do not meet the requirements.
val statusCode = (e as ApiException).statusCode
LogUtils.d("LocationManager -> Device location is close with status code $statusCode, User will get system dialog for enable location")
when (statusCode) {
LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> try {
val rae = e as ResolvableApiException
// Call startResolutionForResult to display a pop-up asking the user to enable related permission.
activity.startIntentSenderForResult(
rae.resolution.intentSender,
REQUEST_CHECK_SETTINGS, null, 0, 0, 0, null
)
} catch (sie: IntentSender.SendIntentException) {
LogUtils.d("LocationKit -> Error while showing system pop-up with error message:" + sie.message)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment