java - 按下按钮时调用 AsyncTask 的问题

标签 java android android-asynctask android-button

我正在开发一个应用程序,它要求我在按下按钮后下载图像。我原本打算使用 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/

相关文章:

android 在选项卡切换上执行操作

android - 异步任务 : onPostExecute is not being invoked

java - 全屏模式下的 JRootPane 大小调整问题

java - MyBatis:选择一个Map进行映射

java - WebViews 的 ViewGroup 历史记录问题

java - 为什么字节图比位图快?

android - 在其他位置显示上下文操作栏

java - Android Studio - 使用 JAR 依赖项(libGDX 项目)时出现 NoClassDefFoundError

Android:由于 AsyncTask 导致内存泄漏

java - 使用 AsyncTask 更新 UI(动态布局)