我已经构建了一个使用位置检测来获取当前位置的应用程序我正在使用LocationManager和LocationLiastener
应用程序工作正常,但当我试图运行它的建议没有安卓播放服务,它不会得到当前位置,并显示以下对话框输入图片描述
这是Github上的应用程序链接https://github.com/mostafa-n3ma/Pcm-helper1.0
通过检查这里的代码https://github.com/mostafa-n3ma/Pcm-helper1.0/blob/master/app/build.gradle#L69
你确定你不依赖于google play服务库吗?
这就是问题所在
private fun checkDeviceLocationSettingsAndGetLocationUpdates(resolve: Boolean = true) {
val locationRequest = com.google.android.gms.location.LocationRequest.create().apply {
priority = LocationRequest.QUALITY_LOW_POWER
}
val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
val settingsClint = LocationServices.getSettingsClient(requireActivity())
val locationSettingsResponseTask = settingsClint.checkLocationSettings(builder.build())
locationSettingsResponseTask.addOnFailureListener { exeption ->
if (exeption is ResolvableApiException && resolve) {
try {
startIntentSenderForResult(
exeption.resolution.intentSender,
REQUEST_TURN_DEVICE_LOCATION_ON,
null,
0, 0, 0,
null
)
} catch (sendEx: IntentSender.SendIntentException) {
Log.d(TAG, "Error geting location settings resolution: " + sendEx.message)
}
} else {
Snackbar.make(
this.requireView(),
"Location services must be enabled to use the app", Snackbar.LENGTH_INDEFINITE
).setAction(android.R.string.ok) {
checkDeviceLocationSettingsAndGetLocationUpdates()
}.show()
}
}
locationSettingsResponseTask.addOnCompleteListener {
if (it.isSuccessful) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0.0f, locationListener
)
}
}
}
我正在使用谷歌服务库来检查位置设置是否启用如果位置处于ON状态,则触发位置更新请求
否则它将要求用户启用它,然后触发位置更新请求
如果服务不可用,什么都不会发生,也不会请求位置更新
所以我将尝试另一种方法来检查位置特性