2011-05-19 56 views
3

嗨,大家好有人可以告訴我,我應該如何使用AsyncTask類加上進度條來執行文件複製到另一個目錄在手機SD卡的本地上下文?我已經看到了一個類似的例子[這裏] [1],但我不知道如何合併差異/修改代碼的上下文以適應我的上下文以使其工作?如何使用AsyncTask類更新複製文件另一個目錄的進度?

回答

6

它會像

// Params are input and output files, progress in Long size of 
// data transferred, Result is Boolean success. 
public class MyTask extends AsyncTask<File,Long,Boolean> { 
    ProgressDialog progress; 

    @Override 
    protected void onPreExecute() { 
    progress = ProgressDialog.show(ctx,"","Loading...",true); 
    } 

    @Override 
    protected Boolean doInBackground(File... files) { 
    copyFiles(files[0],files[1]); 
    return true; 
    } 

    @Override 
    protected void onPostExecute(Boolean success) { 
    progress.dismiss(); 
    // Show dialog with result 
    } 

    @Override 
    protected void onProgressUpdate(Long... values) { 
    progress.setMessage("Transferred " + values[0] + " bytes"); 
    } 
} 

現在,裏面copyFiles你將不得不調用publishProgress()與傳輸的數據的大小,例如。請注意,進度泛型參數是Long。你可以使用commons-io的CountingInputStream包裝器。

有一些額外的東西頂部照顧,但簡而言之就是它。

要啓動:

MyTask task = new MyTask(); 
    task.execute(src,dest); 
+0

@Alex Gitelman感謝您的幫助,但你可以檢查我的新更新的解答有關的另一個問題之上將文件複製到所需目錄後的後果? – Vivian 2011-05-19 15:05:05

+0

@Alex Gitelman順便說一下,我的AsyncTask進度條似乎並不按照它的意圖工作,因爲它應該根據您的代碼?我在代碼中遺漏了其他東西,因此在運行代碼時缺少適當的功能?對不起,我是相當新的android編程... – Vivian 2011-05-19 15:14:57

+0

@Vivian我不明白你的問題'你能檢查我最近更新的答案還有另一個問題'。我很樂意提供幫助,但請更具體。這與這個問題有關嗎?其次,澄清「進度條似乎並不按照預期的方式工作」。我提供的示例將進度條設置爲不確定,以顯示旋轉圓圈。如果這是問題,我會添加另一個片段到我的答案來解決它。 – 2011-05-19 16:34:05

1

,如下圖所示嘗試使用異步任務:

try{ 
    class test extends AsyncTask{ 


     TextView tv_per; 
     int mprogress; 

     Dialog UpdateDialog = new Dialog(ClassContext); 

     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      mprogress = 0; 

      UpdateDialog.setTitle(getResources().getString(R.string.app_name)); 
      UpdateDialog.setContentView(R.layout.horizontalprogressdialog); 
      TextView dialog_message = (TextView)UpdateDialog.findViewById(R.id.titleTvLeft); 
      tv_per = (TextView)UpdateDialog.findViewById(R.id.hpd_tv_percentage); 
      dialog_message.setText(getResources().getString(R.string.dialog_retrieving_data)); 
      dialog_message.setGravity(Gravity.RIGHT); 
      UpdateDialog.setCancelable(false); 
      UpdateDialog.show(); 
      super.onPreExecute(); 
     } 



     @Override 
     protected void onProgressUpdate(Object... values) { 
      // TODO Auto-generated method stub 
      ProgressBar update = (ProgressBar)UpdateDialog.findViewById(R.id.horizontalProgressBar); 
      update.setProgress((Integer) values[0]); 
      int percent = (Integer) values[0]; 
      if(percent>=100) 
      { 
       percent=100; 
      } 
      tv_per = (TextView)UpdateDialog.findViewById(R.id.hpd_tv_percentage); 
      tv_per.setText(""+percent); 
     } 



     @Override 
     protected Object doInBackground(Object... params) { 
      // TODO Auto-generated method stub 
      //your code 
} 

      super.onPostExecute(result); 
      UpdateDialog.dismiss(); 
     } 

    } 
    new test().execute(null); 

} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
} 
+0

感謝您的回答,但雖然我從來沒有實現你的答案,如果你不介意你可以檢查我最近更新的答案,但複製文件成功後還有另一個問題,但它不更新目前新內容的列表視圖?你能幫我解決這個問題嗎? – Vivian 2011-05-19 15:09:25

相關問題