我尝试在我的应用程序中使用 GM API 获取当前位置(使用 Android Studio)。但是,如果我单击触发 getLocation() 函数的按钮,我总是会进入 catch{} 块,但我不知道为什么。我的移动设备已连接以进行测试。
这是 getLocation() 函数:
fun getLocation() {
var locationManager = getSystemService(LOCATION_SERVICE) as LocationManager?
var locationListener = object : LocationListener{
override fun onLocationChanged(location: Location?) {
var latitute = location!!.latitude
var longitute = location!!.longitude
Log.i("test", "Latitute: $latitute ; Longitute: $longitute")
}
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
}
override fun onProviderEnabled(provider: String?) {
}
override fun onProviderDisabled(provider: String?) {
}
}
try {
locationManager!!.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0f, locationListener)
} catch (ex:SecurityException) …Run Code Online (Sandbox Code Playgroud)