2012-06-25 85 views
0

當我登錄到我的應用程序時,異步任務開始執行,當該任務正在執行並且我正在註銷應用程序時,該任務仍在運行並在某段時間後給我結果(儘管我已經登錄出)。我想問一下,有什麼方法可以取消這個任務,這樣它就不會給我結果嗎?取消正在執行的任務

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()登錄聲明不會被調用一些其他類。

+0

check [this](http://stackoverflow.com/questions/2735102/ideal-way-to-cancel-an-executing-asynctask),[this](http://www.technotalkative.com) /取消-的AsyncTask功能於機器人/)。我會和拉利特的回答一起去。 –

+0

會發生什麼,我從Web服務獲取數據。同時運行6個asyntasks。問題是當fisrt異步任務開始執行並註銷時,當我用不同的用戶登錄時,第一個asyntask不會再執行。是否有意義? – Rookie

+0

http://vikaskanani.wordpress.com/2011/08/03/android-proper-way-to-cancel-asynctask/ – Rookie

回答

1

我剛剛在一天前有同樣的問題:)

其他3個答案適用於我的混合物。如果活動

private MyTaskClass miTask; 

在的onCreate /的onResume:

首先聲明你的AsyncTask在現場

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; 
      } 

希望有幫助,問候。 Alex

+0

這是永遠不會isCancelled()。可能是什麼問題呢? – Rookie

+0

isCancelled()總是returingg錯誤:( – Rookie

+0

你可以發佈你的代碼嗎? – Goofyahead

1

是的,有可能。

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

感謝

2

是的,你可以使用cancel(boolean)取消的AsyncTask。您可以創建的AsyncTask類的一個實例,並調用,

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

按本link使用Task.cancel(真);和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...... 
      } 
    } 
}