2013-03-31 60 views
0

據我瞭解,一旦AsyncTask被調用,結果將被改爲null,並且AsyncTask被取消。有沒有辦法保留結果並將其傳遞給onPostExecute(String result)developer.android.com表示不要明確地調用這些函數。在取消AsyncTask時執行onPostExecute

的應用基本上掃描圖像,而如果用戶取消異步任務,我想異步任務,以顯示已掃描圖像。所以結果不應該設置爲null。

這可能實現嗎?如果是,如何?

class openCVOperation extends AsyncTask<String, String, String>{ 
    private MainActivity context = null; 
    /*lots of variables here*/ 
    public openCVOperation(MainActivity context1) { 
     context = context1;// set context from mainActivity 
             // which 
             // inherits Activity super class. 
             // Needed 
             // for accessing widgets. 
    } 

    @Override 
    protected void onPreExecute() { 
     pd = new ProgressDialog(context); 
     pd.setIndeterminate(false); 
     pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     pd.setMax(100); 
     pd.setCancelable(true); 
     pd.setCanceledOnTouchOutside(false); 
     pd.setMessage("Starting up"); 
     pd.show(); 
    } 
    @Override 
    protected String doInBackground(String... params) { 
     publishProgress("Finding path to Storage..."); 
     path = Environment.getExternalStorageDirectory(); 
     p = path.getAbsolutePath(); 
     p=p+"/location"; 
     rm(p);// this has a loop! 
     return null; 

    } 
    @Override 
    protected void onCancelled() 
    { 
     System.out.println("In onCancelled"); 
     super.onCancelled(); 
    } 
@Override 
    protected void onPostExecute(String result) { 
     pd.dismiss(); 
     /*post execute stuff* 
    } 

rm(p)有一個循環,所以我嘗試使用isCancelled()作爲條件,但沒有奏效。

+0

你可以發佈你的代碼嗎?你在哪裏取消你的asynctask? – Raghunandan

+0

我取消了UI中的asynctask。我已經覆蓋了後退按鈕,但它似乎並沒有工作。 –

回答

1

我只是把它添加到我的doInBackground()

pd.setOnCancelListener(new DialogInterface.OnCancelListener() { 

      @Override 
      public void onCancel(DialogInterface dialog) { 
       // TODO Auto-generated method stub 
       task.cancel(true); 
      } 
     }); 

哪裏pd是我的進度對話框。

另外,還要確保你在doInBackground()onCancelled()將永遠不會被調用檢查isCancelled()和應用程序將強制關閉。

1

在doInBackground

if (isCancelled()) 
{ 
    return // image so far 
} 


onPostExecute(String result) 
{ 

    // show result 
} 
+0

這是一個String類型的函數。所以我必須返回一個字符串值。我必須返回什麼?我在'onPostExecute(String result)'中需要很多變量。 –

+0

如果返回字符串,如何獲取掃描圖像? –

+0

'AsyncTask'被取消時'onPostExecute()'不會被調用。 – 323go

-1

如果onCancelled不會被調用,然後你rm方法仍在運行。 因爲如你所說,它運行一個循環。

控制過程(知道是否需要停止),最好的辦法是通過輪詢或乏味檢查你的rm方法中的volatile boolean變量的狀態。

例如,在稱爲cancel的AsyncTask類中創建一個static volatile boolean變量。在onPreExecute方法中將此變量設置爲false

在您的rm方法中,檢查cancel在繁重的任務(打開文件,讀取文件,下載循環的一部分)前後是否爲真。

如果這是真的,那麼打破了return聲明的方法。 更好的是,使您的rm方法返回一個整數,0代表良好,1代表取消。

最後,doInBackground方法命中返回的權利之前,看看你是否需要調用線程或不取消。

public class asyncTask extends AsyncTask<Void, Void, Void> 
{ 
    private static synchronized boolean cancel; 

    protected void onPreExecute() 
    { 
     cancel = false; 
    } 

    protected String doInBackground(Void ... params) 
    { 

     rm(p); 

     if(cancel) 
      asyncTask.cancel; 
     else 
      return null; 
    } 

    protected void onCancelled() 
    { 
     // only executed if doInBackground resulted in a cancel == true 
    } 

    protected void onPostExecute(Void param) 
    { 
     /// only executed if doInBackground resulted in a cancel == false 
    } 

    private int rm(String str) 
    { 
     if(cancel) 
      return 1; 

     //do part of task 

     if(cancel) 
      return 1; 

     //another part of task 

     if(cancel) 
      return 1; 

     //another part of task 

     return cancel ? 1 : 0; 
    } 

} 
0

收集結果:

public class MyTask extends AsyncTask<Void,Void,Void>{ 
    private final List<String> data; 

    public MyTask(){ 
    data = new ArrayList<String>(); 
    } 

    public synchronized List<String> getData(){ 
    return new ArrayList<String>(data); //--current data snapshot-- 
    } 

    private synchronized collect(String s){ 
    data.add(s); 
    }  

    @Override 
    public Void doInBackground(Void...args){ 
    //---do stuff-- 
    collect(/*-stuff-*/); 
    } 
} 

您不會丟失,即使線程被中斷任何東西。

相關問題