Java 在异步任务之前显示对话框
我试图在异步任务开始工作之前显示一个“加载”对话框。在下面的代码中,对话框在所有异步任务完成之前不会显示,只有在最后才会提示对话框 代码如下:Java 在异步任务之前显示对话框,java,android,multithreading,android-asynctask,progressdialog,Java,Android,Multithreading,Android Asynctask,Progressdialog,我试图在异步任务开始工作之前显示一个“加载”对话框。在下面的代码中,对话框在所有异步任务完成之前不会显示,只有在最后才会提示对话框 代码如下: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map); loadMap(); progressBar
@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秒钟才能看到布局,这就是为什么我想用“加载”对话框提示用户。但由于某些原因,用户只能在异步任务操作结束时才能看到它
我该怎么办
编辑:忘记提到在onProgressUpdate中我正在执行一些UI操作(将覆盖图添加到mapview)。这就是UI可能冻结的原因。但我只想知道如何在之前显示加载对话框,我不在乎在“加载”对话框显示后UI是否冻结。事实上我很在乎,但我不知道是否有更好的解决办法
EDIT2:loadmap函数:
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();
}
});
}
您是否尝试过将
loadMap()
移动到asynctask中?在asynctask的onPreExecute()
方法中显示进度条,并在onPostExecute()中取消它。
method@SiddharthLele我已经试过了,我不知道为什么UI会冻结。。即使所有操作都在asynctask线程上。我要重复我自己的话:如果重要的话,asynctask中的操作很繁重,大约需要10秒才能完成。@Siddhartlee只是在UI线程上设置映射,没有什么特别的。我忘了提一件非常重要的事。“我现在要说的是。@Siddhartlele请看一下我的编辑。我怎么做?”?loadmap()正在处理UI对象。