等待直到FusedLocationProviderClient获得最后的位置,然后再继续执行主线程

时间:2019-11-25 19:14:38

标签: android kotlin android-asynctask fusedlocationproviderclient

我正在尝试使用FusedLocationProviderClient获取用户的最后位置,并将这些坐标插入URL中以发出HTTP获取请求。

这是我的onCreate方法中的代码


getLastLocation()

Log.d("weather","weather array size: " + weatherArray?.size)

getLastLocation()

 @SuppressLint("MissingPermission")
    private fun getLastLocation(): String {

        Log.d("weather","in location")

        if(checkPermissions()) {
            Log.d("weather","checked permissions")

            if(isLocationEnabled()) {
                Log.d("weather","checked location enabled")


                mFusedLocationClient.lastLocation.addOnCompleteListener(this) { task ->

                    var location: Location? = task.result
                    if(location == null) {
                        requestNewLocationData()
                        Log.d("weather","LocationCoords was null")
                    }

                    else {

                        Log.d("weather","LocationCoords wasn't null")

                        //latitude and longitude are global variables
                        latitude = location.latitude
                        longitude = location.longitude
                        Log.d("weather","Get Last Location Latitude: " + latitude + " longitude: " + longitude)
                        weatherAPI()
                    }
                }


                Log.d("weather","Finished location client")
            } else {

                Toast.makeText(this, "Turn on LocationCoords", Toast.LENGTH_LONG).show()
                val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
                startActivity(intent)
            }
        } else {
            requestPermissions()
        }

        return "Done location"
    }

首先运行getLastLocation,然后将打印Log.d(“ weather”,“ weather array size:” + weatherArray?.size),然后在其中显示mFusedLocationClient.lastLocation.addOnCompleteListener(位于内部)中的日志消息将显示getLastLocation)。在继续我的代码之前,如何等待mFusedLocationClient完成?

我试图做一个伴侣对象

    companion object {

        class locationAsyncTask internal constructor(context: OutfitCards) : AsyncTask<LocationCoords, Void, LocationCoords?>() {


            private var resp: Weather? = null
            private val activityReference: WeakReference<OutfitCards> = WeakReference(context)
            var mFusedLocationClient: FusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context)
            lateinit var locale: LocationCoords


            override fun onPreExecute() {
                val activity = activityReference.get()
                if (activity == null || activity.isFinishing) return
                //activity.progressBar.visibility = View.VISIBLE
                Log.d("weather","activity finishing")

            }
            override fun doInBackground(vararg params: LocationCoords?): LocationCoords {

                mFusedLocationClient.lastLocation.addOnCompleteListener { task ->

                    var location: Location? = task.result
                    if(location == null) {
                        //requestNewLocationData()
                        Log.d("weather","LocationCoords was null")
                    }

                    else {

                        Log.d("weather","LocationCoords wasn't null")
                        locale = LocationCoords(location.latitude, location.longitude)
                    }
                }

                return locale

            }
        }

但是它从未完成加载,因此应用程序将冻结。 我知道协程,但也无法让它们等待mFusedLocationClient。 任何帮助将不胜感激

0 个答案:

没有答案