取消AsyncTask的Andr​​oid中 [英] Cancel AsyncTask in Android

查看:197
本文介绍了取消AsyncTask的Andr​​oid中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在android系统,以取消的AsyncTask ,但我有我的执行问题:

我的code是:

私有类SynchroTask扩展的AsyncTask {
        私人挥发性布尔运行=真;

  @覆盖
    在preExecute保护无效(){
        super.on preExecute();
        //初始化
    }    @覆盖
    保护无效doInBackground(虚空...... PARAMS){
        而(运行){
            //任务
        }        返回null;
    }    @覆盖
    保护无效onCancelled(){
        super.onCancelled();
        运行= FALSE;
    }    @覆盖
    保护无效onPostExecute(虚空结果){
        super.onPostExecute(结果);
        意图意图= AccountAddActivity.getIntent(getActivity());
        startActivity(意向);
        getActivity()完成()。
        }
    }
}

  mSynchroTask =新SynchroTask();
cancelButton.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){                如果(mSynchroTask = NULL&放大器;!&安培;!mSynchroTask.getStatus()= AsyncTask.Status.FINISHED){
                    HDCApplication.hdcAppManager.mSynchroManager.removeAllTask​​();
                    mSynchroTask.cancel(真);
                    mSynchroTask = NULL;                }
            }        });


解决方案

这是实现你做了什么的一半

  mSynchroTask.cancel(真);


  

一个任务可以在任何时候通过调用取消(布尔)被取消。
  调用此方法将导致isCancelled后续调用()来
  返回true。调用此方法后,onCancelled(对象),而不是
  onPostExecute(对象)会后调用
  doInBackground(Object []对象)的回报。


这是你错过了什么。


  

要确保任务被取消
  尽可能快的,您应经常检查返回值
  如果可能的话isCancelled()定期从doInBackground(对象[]),
  (内部例如一个环路。)


来源: http://developer.android.com/reference/android/ OS / AsyncTask.html

I would like to cancel an Asynctask in android but I have a problem with my implementation :

My code is :

private class SynchroTask extends AsyncTask{ private volatile boolean running = true;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //INITIALIZATIONS
    }

    @Override
    protected Void doInBackground(Void... params) {
        while (running) {
            //TASK
        }

        return null;
    }

    @Override
    protected void onCancelled(){
        super.onCancelled();
        running = false;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        Intent intent = AccountAddActivity.getIntent(getActivity());
        startActivity(intent);
        getActivity().finish();
        }
    }
}

And :

mSynchroTask = new SynchroTask();


cancelButton.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {

                if (mSynchroTask != null && mSynchroTask.getStatus() != AsyncTask.Status.FINISHED){
                    HDCApplication.hdcAppManager.mSynchroManager.removeAllTask();
                    mSynchroTask.cancel(true);
                    mSynchroTask = null;

                }   
            }

        });

解决方案

That's half of implementation of what you have done

   mSynchroTask.cancel(true);

A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns.

This is what you are missing

To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)

Source : http://developer.android.com/reference/android/os/AsyncTask.html

这篇关于取消AsyncTask的Andr​​oid中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆