Dup*_*pla 5 android android-asynctask
我的AsyncTask阻塞块按钮元素,下载图像和进度对话框显示延迟 - 它显示图像显示一段时间,但下载需要很长时间,按钮被阻止(橙色),对话框不显示.
public Bitmap download(String url, ProgressBar progressbar) throws InterruptedException, ExecutionException {
BitmapDownloaderTask task = new BitmapDownloaderTask(progressbar);
task.execute(url);
return task.get();
}
class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
public BitmapDownloaderTask(ProgressBar progressbar) {
}
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(ShowActivity.this);
dialog.setMessage("Loading");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
@Override
protected Bitmap doInBackground(String... Params) {
return imageLoader.getBitmap(params[0]);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
dialog.dismiss();
}
}
Run Code Online (Sandbox Code Playgroud)
在按钮监听器中,只需调用下载功能,进度参数是因为我在imageview中有进度条圆 - 该对话框仅用于测试,找到为什么有延迟和阻塞.在另一个应用程序中我使用runable并且线程和元素没有被阻止,但是在教程中AsyncTask被提到作为更好的解决方案.
big*_*nes 13
图像下载确实是在后台线程中执行的,但是return task.get();你只是等待它完成,这就是阻塞你的主线程.
您应该onPostExecute()将任务完成时用作回调,这样不仅可以关闭对话框,还可以使用返回的位图执行所需操作doInBackground().
| 归档时间: |
|
| 查看次数: |
7097 次 |
| 最近记录: |