Android: как передать current location в AsyncTask
Приложение погода. Через asyntask отправляю запрос в openweathermap и в postexecute начинаю биндить результат в ui, с заранее определенным городом все проходит на ура, добавил функцию запроса геолокации пользователя и сохраняю его в статическое поле, но asynctask выполняется до запроса геолокации, как сделать чтоб чтобы async task работал после запроса данных геолокации
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
getLastLocation()
weatherTask().execute()
}
public class Common {
public static final String APP_ID = "a6g096e9c7045fffc90db97209788939";
public static Location current_location = null;
}
методом сохраняя координаты в common.current_location
mFusedLocationClient.lastLocation.addOnCompleteListener(this) { task ->
val location: Location? = task.result
if (location == null) {
requestNewLocationData()
} else {
Common.current_location.latitude = location.latitude
Common.current_location.longitude = location.longitude
Toast.makeText(this, "${location.latitude}", Toast.LENGTH_LONG).show()
// latTextView.text = location.laлtitude.toString()
// lonTextView.text = location.longitude.toString()
}
}
может как то возможно передать координаты в asynctask
override fun doInBackground(vararg params: String?): String? {
return try{
URL("https://api.openweathermap.org/data/2.5/weather?lat=${Common.current_location.latitude}&lon=${Common.current_location.latitude}&appid=${Common.APP_ID}").readText(
Charsets.UTF_8
)
}catch (e: Exception){
null
}
}