简体   繁体   中英

On android, how to abort geocoder.getFromLocation

A am using the code bellow to get the adress of my lat/lon

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);

The method getFromLocation is bloking. I am already execution in another Thread, but i would like to cancel the operation.

So how do i cancel the getFromLocation ?

Check out the Android documentation for Displaying a Location Address .

Here you create an IntentService, which runs on a worker thread and finishes itself after the onHandleIntent() has completed. They are using a ResultReceiver , that receives the results from geocoder.getFromLocation(lat, lng, 1). The handler passed into the ResultReceiver constructor, would be back on the main/ui thread.

In this example, you wouldn't need to cancel the operation at all. It is running in a service, on a worker thread and therefore not blocking your main/ui thread. Also, the service shuts itself down after it completes geocoder.getFromLocation(lat, lng, 1). If you no longer wanted to receive the Location results back on the main/ui thread, you could easily handle that as well from within the ResultReceiver .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM