2013-06-03 49 views
0

其實我的程序有問題。
我通常必須從FTP服務器上下載文件,並且當點擊我必須下載文件時,我有一個按鈕可以執行此操作。
問題是,當我點擊幾次。 該任務不會運行,因爲我無法設法殺死asyntask。 這裏我已經把一個簡單的例子:如何殺死一個異步任務?

public class MainActivity extends Activity { 


Connexion conx=null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button bt= (Button) findViewById(R.id.button1); 
    bt.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      if (conx!=null){ 
       Log.i("voila", "we are here 1"); 
       conx.cancel(true); 
       conx=new Connexion(); 
       conx.execute("73383_20130426_Tessenderlo_VBR_3.pdf"); 

      }else { 
      conx=new Connexion(); 
      conx.execute("73383_20130426_Tessenderlo_VBR_3.pdf");} 

     } 
    }); 
} 

class Connexion extends AsyncTask<String, String, String> { 
    FTPClient mFTPClient; 
    @Override 
    protected String doInBackground(String... params) { 
     Log.i("voila", "we are here 2"); 
     String chaine = params[0]; 
      try { 
        mFTPClient = new FTPClient(); 
        mFTPClient.connect("site", 21); 
        Log.i("voila", "we are here 4"); 
        if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) { 
         boolean status = mFTPClient.login("user", "pass"); 
         mFTPClient.enterLocalPassiveMode(); 
         ftpDownload("/fromCIS/" +chaine , 
           Environment.getExternalStorageDirectory() 
             + "/Fromcis/" + chaine); 
          mFTPClient.logout(); 
          mFTPClient.disconnect(); 

        } 
       } catch (Exception e) { 

       } 
      return "zaki";  
    } 

    @Override 
    protected void onPostExecute(String result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
     Log.i("voila", "we are here onpost"); 
     conx=null; 

    } 

    public boolean ftpDownload(String srcFilePath, String desFilePath) { 
     boolean status = false; 
     try { 
      FileOutputStream desFileStream = new FileOutputStream(
        desFilePath); 
      ; 
      status = mFTPClient.retrieveFile(srcFilePath, desFileStream); 
      desFileStream.close(); 

      return status; 
     } catch (Exception e) { 
      Log.d(e.getCause() + "", "download failed"); 
     } 

     return status; 
    } 
} 
} 

我必須在我的代碼,以解決我的錯誤增加。
非常感謝你的幫助

非常感謝你的幫助我找到了解決辦法,問題是在retrivefile梅索德,我發現enter link description here

+1

看看[這個答案](http://stackoverflow.com/a/10882600/1289716) – MAC

回答

1

您可以使用task.cancel(true);但通常,如果你在你的doInBackground()一個循環,並在其檢查isCancelled工程價值。 但你的情況下沒有循環。

+0

以及我的情況下必須做些什麼? –

+0

@zakzak爲什麼你點擊幾次? – hasanghaforian

+0

從服務器上下載另一個文件 –

0

task.cancel(true);在這次討論中解決停止的AsyncTask

+1

這只是停止它。它不會殺死任務。 –

+0

當取消'AsyncTask'時,還有其他幾件事情需要考慮。請參閱@hasanghaforian的答案:http://developer.android.com/reference/android/os/AsyncTask.html#cancel%28boolean%29 –

1

,以自定你要做的:

一線從你的主線程:

conx.cancel(true); 

和二線你doInBackgroundMethod(字符串PARAMS ..)

if(this.isCancelled()){ 
return "interrupt" 
} 
0

相反的:

if (conx!=null){ 
     Log.i("voila", "we are here 1"); 
     conx.cancel(true); 
     conx=new Connexion(); 
     conx.execute("73383_20130426_Tessenderlo_VBR_3.pdf"); 
}else { 
     conx=new Connexion(); 
     conx.execute("73383_20130426_Tessenderlo_VBR_3.pdf");} 

只要做到:

// Kill any remaining tasks 
conx = null; 
// Start the new task 
conx=new Connexion(); 
conx.execute("73383_20130426_Tessenderlo_VBR_3.pdf"); 
+0

對不起,我用它,但它不殺asposeask,並在幾個clik我沒有從該計劃的答案 –