【问题标题】:Cancel an executing task取消正在执行的任务
【发布时间】:2012-06-27 01:31:19
【问题描述】:

当我登录到我的应用程序时,异步任务开始执行,而当该任务正在执行并且我注销应用程序时,该任务仍在运行并在一段时间后给我结果(尽管我已经注销)。我想问一下,有没有什么办法可以取消那个任务,让它不给我结果?

  class AsyncClass extends AsyncTask<>{

    @Override
     protected String doInBackground(Void... params)
      {
             if(isCancelled())
               {
                Log.d("isCancelled", iscancelled());
               }
          //call the webservice

      }
    }

现在我正在调用其他一些类

if(asyncTaskObject!=null){
    asyncTaskObject.cancel(true);
        asyncTaskObject=null;
}

但是iscancelled() 中的日志语句永远不会被调用。

【问题讨论】:

  • 检查thisthis。我会接受拉利特的回答。
  • 发生的情况是,我正在从 Web 服务获取数据。同时运行 6 个异步任务。问题是当第一个异步任务开始执行并且我注销时,当我用不同的用户登录时,第一个异步任务不再执行。有意义吗?

标签: android android-asynctask


【解决方案1】:

是的,您可以使用 cancel(boolean) 取消 AsyncTask。您可以创建 AsyncTask 类的实例并调用,

if(task != null && task.equals(AsyncTask.Status.RUNNING))
task.cancel(true);

【讨论】:

    【解决方案2】:

    一天前我也遇到了同样的问题:)

    对我有用的其他 3 个答案的混合体。

    首先在一个字段上声明你的 asyncTask:

    private MyTaskClass miTask;
    

    onCreate/onResume 如果有活动:

    miTask = new MyTaskClass();
    

    然后你可以用任何方法执行它。

    miTask.execute();
    

    在 onPause/onStop 中:

    miTask.cancel(true);
    

    这只有在你的 doInBackground 中检查 isCancelled() 时才有效,这是我为在片段被解除时已经关闭的光标访问所做的示例:

    @Override
        protected Void doInBackground(Void... params) {
            cached = true;
            int idIndex = currentCursor.getColumnIndex(Contacts._ID);
            int displayNameIndex = currentCursor
                    .getColumnIndex(Contacts.DISPLAY_NAME);
    
            while (currentCursor.moveToNext()) {
                if (isCancelled()) {
                    break;
                }
    

    希望对您有所帮助,问候。 亚历克斯

    【讨论】:

    • 永远不会去 isCancelled()。可能是什么问题?
    • isCancelled() 总是返回错误 :(
    • 对不起,我不能……但我可以通过虚拟代码向您展示发生了什么。检查我的编辑
    【解决方案3】:

    是的,有可能。

    YourAsyncTask mTask;
     if(mTask!=null) mTask.cancel(); 
    

    谢谢

    【讨论】:

      【解决方案4】:

      根据link 使用 Task.cancel(true);and isCancelled()

      private class UpdateLibrary extends AsyncTask<Void, Integer, Boolean>{
          private ProgressDialog dialog = new ProgressDialog(Library.this);
          private int total = Library.instance.appState.getAvailableText().length;
          private int count = 0;
      
          //Used as handler to cancel task if back button is pressed
          private AsyncTask<Void, Integer, Boolean> updateTask = null;
      
          @Override
          protected void onPreExecute(){
              updateTask = this;
              dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
              dialog.setOnDismissListener(new OnDismissListener() {               
                  @Override
                  public void onDismiss(DialogInterface dialog) {
                      updateTask.cancel(true);
                  }
              });
              dialog.setMessage("Updating Library...");
              dialog.setMax(total);
              dialog.show();
          }
      
          @Override
          protected Boolean doInBackground(Void... arg0) {
                  for (int i = 0; i < appState.getAvailableText().length;i++){
                      if(isCancelled()){
                          break;
                      }
                      //Do your updating stuff here
                  }
              }
      
          @Override
          protected void onProgressUpdate(Integer... progress){
              count += progress[0];
              dialog.setProgress(count);
          }
      
          @Override
          protected void onPostExecute(Boolean finished){
              dialog.dismiss();
              if (finished)
                  DialogHelper.showMessage(Str.TEXT_UPDATELIBRARY, Str.TEXT_UPDATECOMPLETED, Library.instance);
              else {
                      //Do nothing......
                   }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-20
        • 1970-01-01
        • 1970-01-01
        • 2019-07-10
        • 1970-01-01
        • 1970-01-01
        • 2012-02-13
        相关资源
        最近更新 更多