【发布时间】:2020-02-29 23:31:08
【问题描述】:
我在 Kotlin 中有一个在应用程序中启用 GPS 的功能。是否可以通过添加带有“不再询问”之类的参数的复选框来更改对话框,以避免在用户打开应用程序时一直打开 GPS?
或者也许有另一种解决方案,比如制作自定义对话框?
这是一个代码:
private fun buildAlertMessageNoGps() {
val mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(2000)
.setFastestInterval(1000)
val settingsBuilder = LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest)
.setAlwaysShow(true)
settingsBuilder.setAlwaysShow(true)
val result = LocationServices.getSettingsClient(this).checkLocationSettings(settingsBuilder.build())
result.addOnCompleteListener { task ->
try {
task.getResult(ApiException::class.java)
} catch (ex: ApiException) {
when (ex.statusCode) {
LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> try {
val resolvableApiException = ex as ResolvableApiException
resolvableApiException.startResolutionForResult(this, 100)
} catch (e: IntentSender.SendIntentException) {
Toast.makeText(this,"PendingIntent unable to execute request.",Toast.LENGTH_SHORT).show()
}
LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {
Toast.makeText(
this,
"Something is wrong in your GPS",
Toast.LENGTH_SHORT
).show()
}
}
}
}
}
【问题讨论】:
标签: android service dialog gps location