在android上,如何中止geocoder.getFromLocation

时间:2016-10-18 02:25:20

标签: android multithreading location google-play-services

使用下面的代码来获取我的lat / lon的地址

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

getFromLocation方法很臃肿。我已经在另一个Thread中执行了,但我想取消操作。

那么如何取消getFromLocation?

1 个答案:

答案 0 :(得分:1)

查看Displaying a Location Address的Android文档。

在这里创建一个IntentService,它在一个工作线程上运行,并在onHandleIntent()完成后自行完成。他们正在使用ResultReceiver,它接收来自geocoder.getFromLocation(lat,lng,1)的结果。传递给handler构造函数的ResultReceiver将返回主/ ui线程。

在此示例中,您根本不需要取消操作。它在一个服务中运行,在一个工作线程上运行,因此不会阻塞你的main / ui线程。此外,服务在完成geocoder.getFromLocation(lat,lng,1)后自行关闭。如果您不想再在主/ ui线程上收到位置结果,您也可以在ResultReceiver内轻松处理。

相关问题