每当我使用Geocode类的反向地理编码来获取当前城市名称时,整个用户界面会冻结片刻,直到返回响应。我该如何避免这个问题?
答案 0 :(得分:3)
您可能想要使用AsyncTask
:
private void doGeoCode(Location loc){
AsyncTask<Location,Void,String> task = new AsyncTask<Location,Void,String>(){
@Override
protected String doInBackground(Location... args) {
Location loc = args[0];
String address = "Unknown";
//--do geo code lookup--
return address;
}
@Override
protected void onPostExecute(String address){
//--set some TextView's text etc.
}
};
task.execute(loc);
}