2017-06-23 84 views
0

更新異步任務中的進度欄​​我已經創建了一個應用程序,其中我已經使用Fragment類和另一個Async Class來下載兩個不同的類,現在我必須顯示進度條水平更新進度條在onProgressUpdate中發生了很多下載。但onProgressUpdate我回調它的界面不wrking,它不更新ProgressBar。如何使用界面

FragmentA.class

private void downloadAndUnzipContent(String url){ 
    progressBar.setVisibility(View.VISIBLE); 
    progressBar.setProgress(0); 
    DownloadFileAsync download = new DownloadFileAsync(path+"content.zip", mContext, 
      new DownloadFileAsync.PostDownload(){ 
     @Override 
     public void downloadDone(File file) { 
      Log.i(TAG, "file download completed"); 
     } 
    }, new DownloadFileAsync.ProgressUpdate() { 

     @Override 
     public void ProgressUpdate(int progress) { 
      progressBar.setProgress(progress); 
     } 

    }); 
    download.execute(url); 
} 

在DownloadFileAsync.class這裏我是個下載文件

public class DownloadFileAsync extends AsyncTask<String, Integer, String> { 

private static final String TAG = "DOWNLOADFILE"; 

public static final int DIALOG_DOWNLOAD_PROGRESS = 0; 
private PostDownload callback; 
private ProgressUpdate callProgress; 
private Context context; 
private FileDescriptor fd; 
private File file; 
private String downloadLocation; 

public DownloadFileAsync(String downloadLocation, Context context, PostDownload callback, ProgressUpdate callProgress) { 
    this.context = context; 
    this.callback = callback; 
    this.callProgress = callProgress; 
    this.downloadLocation = downloadLocation; 
} 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
} 

@Override 
protected String doInBackground(String... aurl) { 
    int count; 

    try { 
     URL url = new URL(aurl[0]); 
     URLConnection connection = url.openConnection(); 
     connection.connect(); 

     int lenghtOfFile = connection.getContentLength(); 
     Log.d(TAG, "Length of the file: " + lenghtOfFile); 

     InputStream input = new BufferedInputStream(url.openStream()); 
     file = new File(downloadLocation); 
     FileOutputStream output = new FileOutputStream(file); //context.openFileOutput("content.zip", Context.MODE_PRIVATE); 
     Log.d(TAG, "file saved at " + file.getAbsolutePath()); 
     fd = output.getFD(); 

     byte data[] = new byte[1024]; 
     long total = 0; 
     while ((count = input.read(data)) != -1) { 
      total += count; 
      publishProgress((int) ((total * 100)/lenghtOfFile)); 
      output.write(data, 0, count); 
     } 

     output.flush(); 
     output.close(); 
     input.close(); 
    } catch (Exception e) { 
    } 
    return null; 

} 

protected void onProgressUpdate(Integer... progress) { 
    //Log.d(TAG,progress[0]); 
    if(callProgress != null) 
     callProgress.ProgressUpdate(progress[0]); 
} 

@Override 
protected void onPostExecute(String unused) { 
    //progressBar.setVisibility(View.GONE); 
    if (callback != null) callback.downloadDone(file); 
} 

public interface PostDownload { 
    void downloadDone(File fd); 
} 

public interface ProgressUpdate { 
    void ProgressUpdate(int progress); 
} 
} 

請幫我在那裏,我在代碼

感謝名單做了錯誤提前

+0

https://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog – Vij

+0

有沒有需要的接口,它可以是直接從異步任務類 –

+2

完成,你只需要通過一個活動來保存片段的UI元素的引用,並且可以直接更新異步類的onProgressUpdate的進度條,因爲你有上下文變量 –

回答

-1

您無法修改UI線程以外的用戶界面。快速解決可能是:

yourFragment.getActivity().runOnUiThread(new Runnable() 
    { 
     @Override 
     public void run() 
     { 
      progressBar.setProgress(progress); 
     } 
    }); 
0

你的代碼是沒問題的。 請調試檢查

int lenghtOfFile = connection.getContentLength(); 

的 「lenghtOfFile」 是正確的。 並非所有的服務器都返回到ContentLength。 有時候它是-1。