Async Task is Deprecated -- Geocode Address -- Get Address f



这是我在Android Studio与Kotlin工作的一个个人项目。我很难理解如何为获得地理代码地址的Asynctask修复此弃用的代码。如果有人能帮我找到一个不同的方法来实现这个目标,我将非常感激。

如果有什么我可以提供,使这个问题更清楚,请让我知道!

class GetAddressFromLatLng(
context: Context, private val latitude: Double,
private val longitude: Double) : AsyncTask<Void, String, String>() {
/**
* Constructs a Geocoder whose responses will be localized for the
* given Locale.
*
* @param context the Context of the calling Activity
* @param locale the desired Locale for the query results
*
* @throws NullPointerException if Locale is null
*/
private val geocoder: Geocoder = Geocoder(context, Locale.getDefault())
/**
* A variable of address listener interface.
*/
private lateinit var mAddressListener: AddressListener
/**
* Background method of AsyncTask where the background operation will be performed.
*/
@Deprecated("Deprecated in Java")
override fun doInBackground(vararg params: Void?): String {
try {
/**
* Returns an array of Addresses that are known to describe the
* area immediately surrounding the given latitude and longitude.
*/
val addressList: List<Address>? = geocoder.getFromLocation(latitude, longitude, 1)
if (addressList != null && addressList.isNotEmpty()) {
val address: Address = addressList[0]
val sb = StringBuilder()
for (i in 0..address.maxAddressLineIndex) {
sb.append(address.getAddressLine(i)).append(",")
}
sb.deleteCharAt(sb.length - 1) // Here we remove the last comma that we have added above from the address.
return sb.toString()
}
} catch (e: IOException) {
Log.e("HappyPlaces", "Unable connect to Geocoder")
}
return ""
}
/**
* onPostExecute method of AsyncTask where the result will be received and assigned to the interface accordingly.
*/
@Deprecated("Deprecated in Java")
override fun onPostExecute(resultString: String?) {
if (resultString == null) {
mAddressListener.onError()
} else {
mAddressListener.onAddressFound(resultString)
}
super.onPostExecute(resultString)
}
/**
* A public function to set the AddressListener.
*/
fun setAddressListener(addressListener: AddressListener) {
mAddressListener = addressListener
}
/**
* A public function to execute the AsyncTask from the class is it called.
*/
fun getAddress() {
execute()
}
/**
* A interface for AddressListener which contains the function like success and error.
*/
interface AddressListener {
fun onAddressFound(address: String?)
fun onError()
}

}

您可以使用Handler。

class GetAddrFromLatLog(
context: Context,
private val latitude: Double,
private val longitude: Double
) : Runnable {
private val geocoder: Geocoder = Geocoder(context, Locale.getDefault())
private var addressListener: AddressListener? = null
override fun run() {
try {
val addressList = geocoder.getFromLocation(latitude, longitude, 1)
if (addressList != null || addressList.isNotEmpty()) {
val address: Address = addressList[0]
val sb = StringBuilder()
for (i in 0..address.maxAddressLineIndex) {
sb.append(address.getAddressLine(i)).append(",")
}
sb.deleteCharAt(sb.length - 1) // Here we remove the last comma that we have added above from the address.
addressListener?.onAddressFound(sb.toString())
} else {
addressListener?.onError()
}
} catch (e: IOException) {
Log.e("HappyPlaces", "Unable connect to Geocoder")
addressListener?.onError()
}
}
fun setAddressListener(callback: AddressListener?) {
addressListener = callback
}
interface AddressListener {
fun onAddressFound(address: String?)
fun onError()
}
}
// You functionto call `GetAddrFromLatLog` class
fun getGeoAddress(callback: AddressListener?) {
val th = HandlerThread("getAddr")
th.start()

val handler = Handler(th.getLooper())
handler.post(GetAddrFromLatLog(context, lat, log).apply {
setAddressListener(callback)
})
}

我认为你也可以用RxJava来代替AsyncTask

最新更新