4

嗨,我是 android 新手:

我在 GridView 中显示图像缩略图。为了获得更好的性能,我异步加载它。

我的 AsyncTask 是:

class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
        private final WeakReference<ImageView> imageViewReference;
        private int data = 0;
        private String image_path;

        public BitmapWorkerTask(ImageView imageView) {
            imageViewReference = new WeakReference<ImageView>(imageView);
        }

        @Override
        protected Bitmap doInBackground(Integer... params) {
            data = params[0];
            Bitmap picture = BitmapFactory.decodeFile(image_path);
            int width = picture.getWidth();
            int height = picture.getHeight();
            float aspectRatio = (float) width / (float) height;
            int newWidth = 98;
            int newHeight = (int) (98 / aspectRatio);
            return picture = Bitmap.createScaledBitmap(picture, newWidth,
                    newHeight, true);

        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (imageViewReference != null && bitmap != null) {
                final ImageView imageView = imageViewReference.get();
                if (imageView != null) {
                    imageView.setImageBitmap(bitmap);
                }

            }
        }

        public void onDismiss(DialogInterface dialog) {
            this.cancel(true);
        }
    }

和调用形式:

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

        LayoutInflater layoutInflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ListRowHolder listRowHolder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.ll_sponsor_list_item,
                    parent, false);
            listRowHolder = new ListRowHolder();
            listRowHolder.imgSponsor = (ImageView) convertView
                    .findViewById(R.id.imggrid_item_image);
            convertView.setTag(listRowHolder);

        } else {
            listRowHolder = (ListRowHolder) convertView.getTag();
        }
        try {
            BitmapWorkerTask task = new BitmapWorkerTask(
                    listRowHolder.imgSponsor);
            task.image_path = ImageName.get(position);
            task.execute(1);

        } catch (Exception e) {
            Toast.makeText(ctx, e + "", Toast.LENGTH_SHORT).show();
        }

        return convertView;
    }

这里的问题是即使我点击后退按钮,任务也在后台运行。

4

3 回答 3

15

用于cancel()停止AsyncTask

task.cancel(true);

AsyncTask的文档在“取消任务”部分提供了一些额外的细节:

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

于 2013-01-11T14:27:36.627 回答
5

首先,您需要保留对您执行的每个异步任务的引用。当活动暂停时,您应该遍历对 asynctask 的引用并使用cancel()取消它们。您还应该在每个异步任务上调用get() 。这将确保 UI 线程等到异步任务完成后再更改活动。

如果您希望异步任务的线程被中断,请使用 cancel(true),或者使用 cancel(false),并且在您的 doInBackground() 方法中,您应该检查isCancelled()并返回。

为了安全起见,您必须非常小心异步任务。查看这篇关于安全处理它们的文章。

于 2013-01-11T14:33:08.853 回答
0

做这样的事情

private class myAsyncTask extends AsyncTask<Void, String, Void> {
          private boolean isTaskCancelled = false;

          public void cancelTask(){
               isTaskCancelled = true;
          }

          private boolean isTaskCancelled(){
               return isTaskCancelled;
          }

          protected Void doInBackground( Void... ignoredParams ) {
            //Do some stuff
             if (isTaskCancelled()){
                return;
             }
          }


          protected void onPostExecute( Void array )
          {
           //Do something
          }



          protected void onProgressUpdate(String... values)
          {
           //Do something
          }
    }

当您按下 Activity 时,将 asynctask 置于取消模式,这样它将停止执行。

检查您在 asynctask 中执行的每个步骤中的取消方法,以在下一行停止它

于 2013-01-11T14:26:33.297 回答