當服務器正在尋找谷歌地圖上的地址時,當進度完成時,對話框消失,我想顯示進度對話框。我google了它,結果大部分是談論AsyncTask,但我仍然對函數doInBackground()的參數和onPreExecute()和onPostExecute()的用法感到困惑。有人可以給我一些解決方案。我非常感謝任何幫助,謝謝。在使用AsyncTask搜索谷歌地圖時顯示進度對話框
protected void mapCurrentAddress() {
String addressString = addressText.getText().toString();
Geocoder g = new Geocoder(this);
List<Address> addresses;
try {
addresses = g.getFromLocationName(addressString, 1);
if (addresses.size() > 0) {
address = addresses.get(0);
List<Overlay> mapOverlays = mapView.getOverlays();
AddressOverlay addressOverlay = new AddressOverlay(address);
mapOverlays.add(addressOverlay);
mapView.invalidate();
final MapController mapController = mapView.getController();
mapController.animateTo(addressOverlay.getGeopoint(), new Runnable() {
public void run() {
mapController.setZoom(12);
}
});
useLocationButton.setEnabled(true);
} else {
// show the user a note that we failed to get an address
alert(this, addressString);
}
} catch (IOException e) {
// show the user a note that we failed to get an address
e.printStackTrace();
}
}
private class SearchAddress extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(AddLocationMapActivity.this);
// can use UI thread here
@Override
protected void onPreExecute() {
this.dialog.setTitle("Checking");
this.dialog.setMessage("Contacting Map Server...");
this.dialog.show();
}
// automatically done on worker thread (separate from UI thread)
@Override
protected Void doInBackground(Void... params) {
try {
mapCurrentAddress();
}
catch(Exception e) {
Log.e("DEBUGTAG", "Remote Image Exception", e);
}
return null;
}
// can use UI thread here
@Override
protected void onPostExecute(Void res) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
}
// this is the click event
mapLocationButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new SearchAddress().execute(); // crash during doing the doInBackground
//mapCurrentAddress(); // work perfectly
}
});
彈出進度對話框後,應用程序崩潰?
檢查[這](HTTP:// WWW .technotalkative.com/android-google-image-search-api-example-json-parsing-web-api-call-demo /)和[this](http://www.technotalkative.com/loading-remote-images) /)實例。 – 2012-04-02 08:57:21