终止android中正在运行的下载异步任务

终止android中正在运行的下载异步任务,android,asynchronous,android-fragments,android-asynctask,tabwidget,Android,Asynchronous,Android Fragments,Android Asynctask,Tabwidget,我正在开发一个图像下载程序,它是一个异步下载程序。每当需要在互联网上显示图像时,我都会调用它 图像异步下载程序(输入为目标图像视图、图像url) 适配器: gridView.setAdapter(new GalleryAdapter(getActivity() , images)); public class GalleryAdapter extends BaseAdapter { private Context mContext; public ArrayList<Ga

我正在开发一个图像下载程序,它是一个异步下载程序。每当需要在互联网上显示图像时,我都会调用它

图像异步下载程序(输入为目标图像视图、图像url)

适配器:

gridView.setAdapter(new GalleryAdapter(getActivity() , images));
public class GalleryAdapter extends BaseAdapter {
    private Context mContext;
    public ArrayList<GalleryImage> images;

    // Constructor
    public GalleryAdapter(Context c, ArrayList<GalleryImage> _images) {
        mContext = c;
        images = _images;
    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public Object getItem(int position) {
        return images.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if( convertView == null ){
            convertView = (ImageView)new ImageView(mContext);
            int size = (int)(Utility.getScreenWidth(mContext) / 3) - 1; 
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(size, size);
            convertView.setLayoutParams(params);
            convertView.setBackgroundResource(android.R.color.darker_gray);
            convertView.getBackground().setAlpha(204); // = 0.8 alpha
        }

        new ImageLoader().execute(convertView,images.get(position).thumbUrl);

        return convertView;
    }
}
公共类GalleryAdapter扩展了BaseAdapter{
私有上下文;
公共阵列图像;
//建造师
公共GalleryAdapter(上下文c、ArrayList\u图像){
mContext=c;
图像=_图像;
}
@凌驾
public int getCount(){
返回图像。size();
}
@凌驾
公共对象getItem(int位置){
返回图像。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
convertView=(ImageView)新的ImageView(mContext);
int size=(int)(Utility.getScreenWidth(mContext)/3)-1;
AbsListView.LayoutParams params=新的AbsListView.LayoutParams(大小,大小);
convertView.setLayoutParams(参数);
setBackgroundResource(android.R.color.深灰色);
convertView.getBackground().setAlpha(204);/=0.8 alpha
}
新建ImageLoader().execute(convertView,images.get(position.thumbUrl));
返回视图;
}
}

我想知道当我更改标签时有没有办法取消下载任务?(由于用户在所有下载完成之前就离开了选项卡,因此不再需要下载了)

可以随时通过调用来取消任务。调用此方法将导致后续调用返回true。调用此方法后,将在返回后调用而不是为确保尽快取消任务,应始终定期从
doInBackground(Object[])
检查
isCancelled()
的返回值,如果可能的话(例如在循环内)。

在您的情况下,
doInBackground()
代码中没有循环。事实上,如果通过读取缓冲的响应流将其转换为一个循环会更好。这将提高性能,使您能够在循环中调用
isCancelled()

protected Bitmap doInBackground(Object... params) {
            try {

                URL url = new URL(imageURL);
                HttpURLConnection connection = (HttpURLConnection) url
                        .openConnection();
                input = connection.getInputStream();

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] byteChunk = new byte[4096];
                int n;

                while ( (n = input.read(byteChunk)) > 0 ) {
                     if(isCancelled()) {
                        return null;
                     }
                     baos.write(byteChunk, 0, n);
                }

                Bitmap myBitmap = BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.size());

                return myBitmap;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            } finally {
                try {
                    if (input != null)
                        input.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

   }
示例:用于读取块中的字节,并定期在循环中检查
isCancelled()

protected Bitmap doInBackground(Object... params) {
            try {

                URL url = new URL(imageURL);
                HttpURLConnection connection = (HttpURLConnection) url
                        .openConnection();
                input = connection.getInputStream();

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] byteChunk = new byte[4096];
                int n;

                while ( (n = input.read(byteChunk)) > 0 ) {
                     if(isCancelled()) {
                        return null;
                     }
                     baos.write(byteChunk, 0, n);
                }

                Bitmap myBitmap = BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.size());

                return myBitmap;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            } finally {
                try {
                    if (input != null)
                        input.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

   }
当然,您应该保留对AsyncTask对象的引用,以便能够对其调用cancel方法

    private ImageLoader loader;
    ...
    ...
    loader = new ImageLoader();
    loader.execute();
    ...
    ...
    loader.cancel()

您介意提供一些更详细的代码示例吗?谢谢