我正在开发一个应用程序,它要求我在按下按钮后下载图像。我原本打算使用 AsyncTask,直到我发现只能调用特定的 AsyncTask 一次。我应该使用什么来代替,以便我仍然可以使用进度对话框等,但仍然可以在按下按钮时调用它?
按下按钮时调用,传入一个 int
class ImageDownloader
extends AsyncTask<Integer, Integer, Bitmap> {
protected void onPreExecute(){
launchDialog();
}
@Override
protected Bitmap doInBackground(Integer... params) {
//TODO Auto-generated method stub
try{
//finding and downloading an image, and passing back the proper bitmap to the onPostExecute
}catch(Exception e){
Log.e("Image", "Failed to load image", e);
}
return null;
}
protected void onProgressUpdate(Integer... params){
}
protected void onPostExecute(Bitmap img){
ImageView iv = (ImageView) findViewById(R.id.imageView);
if(iv!=null && img!=null){
iv.setImageBitmap(img);
new PhotoViewAttacher(iv);
}
closeDialog();
enablebuttons();
}
protected void onCancelled(){
closeDialog();
enablebuttons();
}
}
提前致谢!
最佳答案
每次只需在点击处理程序中创建一个新的 AsyncTask
实例并运行它(而不是一遍又一遍地执行单个实例)。
关于java - 按下按钮时调用 AsyncTask 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21566473/