Java Android对话框和AsyncTask导致UI冻结
因此,在我的对话框中,我有一个按钮,启动AsyncTaskDownloader从我的服务器获取一些文件;这很好用。但是,我想取消当前对话框,并在单击按钮时显示ProgressDialog。我将如何处理这个问题 此时,如果我单击按钮(对话框中的一个元素),当下载程序从服务器下载文件时,整个UI都会冻结。下载程序完成任务后,将显示进度对话框 我的代码看起来像这样Java Android对话框和AsyncTask导致UI冻结,java,android,multithreading,android-asynctask,android-ui,Java,Android,Multithreading,Android Asynctask,Android Ui,因此,在我的对话框中,我有一个按钮,启动AsyncTaskDownloader从我的服务器获取一些文件;这很好用。但是,我想取消当前对话框,并在单击按钮时显示ProgressDialog。我将如何处理这个问题 此时,如果我单击按钮(对话框中的一个元素),当下载程序从服务器下载文件时,整个UI都会冻结。下载程序完成任务后,将显示进度对话框 我的代码看起来像这样 MainActivity{ Dialog dialog; ProgressDialog progress; o
MainActivity{
Dialog dialog;
ProgressDialog progress;
onCreate(){
dialog = new Dialog(this);
progress = new ProgressDialog(this);
dialog.setContentView(Some View Resource With My Button);
someMethod();
dialog.show();
}
someMethod(){
Button button = (Button) dialog.findViewById(resource of button);
button.setOnClickListener(new OnClickListener(){
onClick(){
dialog.dismiss();
progress.show();
new DownloaderAsyncTask(progress).execute().get();
}
//go to another activity when download finished
});
}
private class DownloaderAsyncTask extends AsyncTask<Void, Void, Void>{
ProgressDialog progress;
DownloaderAsyncTask(ProgressDialog progress){
this.progress = progress;
}
doInBackGround(){
//Downloading
}
onPostExecute(){
//Kill connection
this.progress.dismiss();
}
}
}
main活动{
对话;
进程对话进程;
onCreate(){
dialog=新建对话框(此对话框);
进度=新建进度对话框(此对话框);
setContentView(使用我的按钮查看资源);
somethod();
dialog.show();
}
someMethod(){
Button Button=(Button)dialog.findViewById(按钮的资源);
setOnClickListener(新的OnClickListener(){
onClick(){
dialog.dismise();
progress.show();
新下载的erasynctask(progress.execute().get();
}
//下载完成后转到其他活动
});
}
私有类下载同步任务扩展异步任务{
进程对话进程;
下载Erasynctask(进度对话框进度){
这个。进步=进步;
}
doInBackGround(){
//下载
}
onPostExecute(){
//终止连接
this.progress.discouse();
}
}
}
谢谢。如果你们需要更多的信息,请告诉我
new DownloaderAsyncTask(progress).execute().get();
我真的不知道为什么AsyncTask
有get()
方法——它基本上把异步进程变成了同步进程,因为它会阻塞并等待结果。这就是UI冻结的原因
如果您想等待下载程序完成,然后执行其他操作,这就是onPostExecute(…)
的作用
我真的不知道为什么AsyncTask
有get()
方法——它基本上把异步进程变成了同步进程,因为它会阻塞并等待结果。这就是UI冻结的原因
如果您想等待下载程序完成,然后执行其他操作,这就是
onPostExecute(…)
的作用。您的UI线程被冻结,因为您调用了get()
。不要那样做。相反,启动您的AsyncTask
,并使您的onPostExecute()
调用包含下载结束时希望执行的代码的方法,如下所示(更多/更少):
someMethod(){
Button Button=(Button)dialog.findViewById(按钮的资源);
setOnClickListener(新的OnClickListener(){
onClick(){
dialog.dismise();
progress.show();
新下载的erasynctask().execute();
}
});
私有void下载程序finished(){
this.progress.discouse();
///进入下一个活动。
}
私有类下载同步任务扩展异步任务{
doInBackGround(){
//下载
}
onPostExecute(){
//终止连接
下载完成();
}
}
这就是为什么要这么做,因为
async
代表异步,所以调用get()
会扼杀所有的好处。您的UI线程被冻结,因为您调用了get()
。不要这样做。相反,启动异步任务
并使您的在后期执行()
call方法,其中包含下载结束时希望执行的代码,如下所示(更多/更少):
someMethod(){
Button Button=(Button)dialog.findViewById(按钮的资源);
setOnClickListener(新的OnClickListener(){
onClick(){
dialog.dismise();
progress.show();
新下载的erasynctask().execute();
}
});
私有void下载程序finished(){
this.progress.discouse();
///进入下一个活动。
}
私有类下载同步任务扩展异步任务{
doInBackGround(){
//下载
}
onPostExecute(){
//终止连接
下载完成();
}
}
这就是它的实现方式,因为
async
代表异步,所以调用get()
取消所有好处。没有错误。UI被冻结,因为它正在等待下载程序完成。下载程序完成后,一切正常。但是,我希望我当前的对话框被取消,并在单击按钮后立即显示progressDialog。不要调用get()
。查看答案。没有错误。UI被冻结,因为它正在等待下载程序完成。下载程序完成后,一切正常。但是,我希望我当前的对话框被取消,并在单击按钮后立即显示progressDialog。不要调用get()
。查看答案。
someMethod() {
Button button = (Button) dialog.findViewById(resource of button);
button.setOnClickListener(new OnClickListener(){
onClick(){
dialog.dismiss();
progress.show();
new DownloaderAsyncTask().execute();
}
});
private void downloaderFinished() {
this.progress.dismiss();
/// go to next activity.
}
private class DownloaderAsyncTask extends AsyncTask<Void, Void, Void>{
doInBackGround(){
//Downloading
}
onPostExecute(){
//Kill connection
downloaderFinished();
}
}