我正在构建一个应用程序,该应用程序将使用 GeoCoder 从用户的输入进行街道寻址。这是我做的一段代码:
Geocoder gc = new Geocoder(getBaseContext(), Locale.getDefault());
List<Address> addresses = null;
StringBuilder sb = new StringBuilder();
String destination = edittext_destination.getText().toString();
try {
addresses = gc.getFromLocationName(destination, 10);
} catch (Exception e){
Toast.makeText(getBaseContext(), "Address not found", Toast.LENGTH_SHORT).show();
}
上面的代码正在运行,但返回结果需要一些时间。在等待结果时,我想显示进度微调器。我知道它应该使用线程,但我不知道如何开始。我希望任何人都可以提供帮助。
谢谢
答案 0 :(得分:1)
您可以使用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);
答案 1 :(得分:0)
Android Query使所有这些Async的东西变得非常容易。你可以添加微调器和进度条,只需要一个像这样的异步调用:
设定:
ProgressDialog dialog = new ProgressDialog(this);
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.setInverseBackgroundForced(false);
dialog.setCanceledOnTouchOutside(true);
dialog.setTitle("Sending...");
String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";
你是异步电话:
aq.progress(dialog).ajax(url, JSONObject.class, this, "jsonCb");
看看吧!出色的响应时间和Peter Liu的帮助。这个项目有益于Devs!