你可以这样做AsyncTask
:
final String destination = edittext_destination.getText().toString();
new AsyncTask<String, Void, List<Address>>() {
private Dialog loadingDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
loadingDialog = ProgressDialog.show(TestActivity.this, "Please wait", "Loading addresses...");
}
@Override
protected List<Address> doInBackground(String... params) {
String destination = params[0];
try {
Geocoder gc = new Geocoder(getBaseContext(),
Locale.getDefault());
return gc.getFromLocationName(destination, 10);
} catch (Exception e) {
return null;
}
}
@Override
protected void onPostExecute(List<Address> addresses) {
loadingDialog.dismiss();
if (addresses == null) {
Toast.makeText(getBaseContext(), "Geocoding error",
Toast.LENGTH_SHORT).show();
} else if (addresses.size() == 0) {
Toast.makeText(getBaseContext(), "Address not found",
Toast.LENGTH_SHORT).show();
} else {
// Do UI stuff with your addresses
Toast.makeText(getBaseContext(), "Addresses found: " + addresses.size(), Toast.LENGTH_SHORT).show();
}
}
}.execute(destination);