在asynctask之前显示对话框

时间:2012-09-29 15:41:33

标签: java android multithreading android-asynctask progressdialog

我正在尝试在异步任务开始工作之前显示“正在加载”对话框。使用以下代码会发生的事情是对话框没有显示,直到所有异步任务都完成了他的操作,对话框最后才会被提示。

以下是代码:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    loadMap();
    progressBar = ProgressDialog.show(Map.this, "", 
            "Loading. Please wait...", true);

    //firing the asynctask here
}

asynctask正在进行相当繁重的操作,需要10秒才能看到布局,这就是我想用“加载”对话框提示用户的原因。但是用户只能在asynctask操作结束时才能看到它。

我该怎么办?

编辑:忘记在onProgressUpdate中提到我正在做一些UI操作(将叠加层添加到mapview)。这就是UI可能冻结的原因。但我只是想知道如何显示加载对话框,我不在乎在“加载”对话框显示后UI是否冻结。其实我很在意,但我不知道是否有更好的解决方案。

EDIT2:加载图功能:

    public void loadMap()
{

        mapView = (TapControlledMapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapController = mapView.getController();
        mapController.animateTo(new GeoPoint((int)(47.975 * 1E6), (int)(17.056 * 1E6)));
        mapView.setOnSingleTapListener(new OnSingleTapListener() {      
            @Override
            public boolean onSingleTap(MotionEvent e) {
                itemizedOverlay.hideAllBalloons();
                return true;
            }
        });
        myLocationOverlay = new FixedMyLocationOverlay(this, mapView);
        mapView.getOverlays().add(myLocationOverlay);
        mapView.invalidate();
        zoomToMyLocation();

        editText = (EditText)findViewById(R.id.search);
        editText.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus)
                onSearchRequested();
                EditText editText = (EditText)v;
                editText.clearFocus();

            }
        });

}

1 个答案:

答案 0 :(得分:-1)

您是否尝试将loadMap()移至asynctask?

相关问题