【发布时间】:2021-09-27 23:03:57
【问题描述】:
当我尝试在不进入手机设置的情况下打开 gps 时,我看到了这个对话框:
这是我的代码示例:
viewModel.onEnableGpsEvent.observe(this, Observer { event ->
event.getContentIfNotHandled()?.let {
activity?.let {
val locationRequest = LocationRequest.create()
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
val task = LocationServices.getSettingsClient(it).checkLocationSettings(builder.build())
task.addOnSuccessListener { response ->
val states = response.locationSettingsStates
if (states.isLocationPresent) {
//TODO: Do something if need it
}
}
task.addOnFailureListener { e ->
if (e is ResolvableApiException) {
try {
// Handle result in onActivityResult()
e.startResolutionForResult(
it,
LOCATION_SETTING_REQUEST
)
} catch (sendEx: IntentSender.SendIntentException) {
}
}
}
}
我还有下一个依赖:
implementation 'com.google.android.gms:play-services-location:18.0.0'
【问题讨论】:
标签: android kotlin android-location android-gps