代码之家  ›  专栏  ›  技术社区  ›  ant2009

尝试获取最后一个位置更新时调用requestLocationUpdates时尝试超时

  •  0
  • ant2009  · 技术社区  · 7 年前

    Android Studio 3.4

    FusedLocationProviderClient 得到纬度和纬度的坐标。

    在下面的代码片段中,我试图通过请求位置更新来处理这个场景。但是,该应用程序将不会得到任何最后的更新,我想请求超时。我创造了 LocationResult setExpirationDuration 我想如果我把它设为10000毫秒,10秒后可能会超时,但什么也没发生。

     private fun getLocationFused() {
            fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
    
            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) !=
                    PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) !=
                    PackageManager.PERMISSION_GRANTED) {
    
                ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION), permissionRequestCode)
            }
            else {
                fusedLocationProviderClient.lastLocation.addOnSuccessListener(this) { location ->
                    if(location != null) {
                        forecastPresenter.requestWeatherForecast(location.latitude, location.longitude)
                    }
                    else {
                        val locationCallback = object : LocationCallback() {
                            override fun onLocationResult(locationResult: LocationResult) {
                                forecastPresenter.requestWeatherForecast(locationResult.lastLocation.latitude, locationResult.lastLocation.longitude)
                                fusedLocationProviderClient.removeLocationUpdates(this)
                            }
                        }
    
                        val locationResult = LocationRequest()
                        locationResult.maxWaitTime = 10000
                        fusedLocationProviderClient.requestLocationUpdates(locationResult, locationCallback, null)
                    }
                }.addOnFailureListener(this) {
                    println(it.message)
                }.addOnCanceledListener(this) {
                    println("Cancelled")
                }
            }
        }
    

    如果事件在之后超时,将调用什么方法?会不会 onLocationResult 以nul返回 locationResult

    非常感谢您的建议,

    0 回复  |  直到 7 年前