2016-12-06 73 views
0

我嘗試將文件加載到服務器。我需要等待所有操作完成之後才能開始使用其他方法。所以我使用同步呼叫。我嘗試在啓動新線程之前顯示ProgressDialog,但是,當所有線程都沒有完成時,我的UI線程就卡住了。ProgressDialog在新線程之前不啓動線程

private void uploadImageSync() { 
    final ProgressDialog progressDialog; 
    progressDialog = new ProgressDialog(BarcodActivity.this); 
    progressDialog.setMessage("Load..."); 
    progressDialog.show(); 

    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(ROOT_URL) 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 

    RequestInterface service = retrofit.create(RequestInterface.class); 

    int i=0; 
    while (i++ <= 4) { 
     File f = getOutputMediaFilePath(mCode + "_"+i, true); 
     if(f.exists()){ 
      RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), f); 

      MultipartBody.Part body = MultipartBody.Part.createFormData("file", f.getName(), requestFile); 

      final Call<ResponseBody> resultCall = service.uploadImage(body); 

      Thread t = new Thread(new Runnable() { 
       public void run() { 
        try { 
         resultCall.execute().body(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       }}); 
      t.start(); 
      try { 
       t.join(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
    progressDialog.dismiss(); 
} 

回答

0

你的UI線程凍結的原因是你打電話給t.join()。您的UI Thread一直等到新的Thread完成。

相反,您可以使用AsyncTask,因爲該類是爲這樣的任務而製作的。

下面是關於如何使用它的一般示例和android dev guide

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { 
    protected Long doInBackground(URL... urls) { 
     int count = urls.length; 
     long totalSize = 0; 
     for (int i = 0; i < count; i++) { 
      totalSize += Downloader.downloadFile(urls[i]); 
      publishProgress((int) ((i/(float) count) * 100)); 
      // Escape early if cancel() is called 
      if (isCancelled()) break; 
     } 
     return totalSize; 
    } 
protected void onProgressUpdate(Integer... progress) { 
    setProgressPercent(progress[0]); 
} 

protected void onPostExecute(Long result) { 
    showDialog("Downloaded " + result + " bytes"); 
    } 
} 

new DownloadFilesTask().execute(url1, url2, url3); 

注意,在UI線程onPostExecute運行,所以你可以很容易地解僱在那裏。

0
private class DownloadFileTask extends AsyncTask<Void, Void, Void> { 

    private ProgressDialog progressDialog = null; 
    private WeakReference<Activity> weakReference = null; 

    public DownloadFileTask(Activity activity) { 
     reference = new WeakReference<>(activity); 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     progressDialog = new ProgressDialog(weakReference.get()); 
     progressDialog.setMessage("Load..."); 
     progressDialog.show(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl(ROOT_URL) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 
     RequestInterface service = retrofit.create(RequestInterface.class); 

     int i=0; 
     while (i++ <= 4) { 
      File f = getOutputMediaFilePath(mCode + "_"+i, true); 
      if(f.exists()){ 
       RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), f); 
       MultipartBody.Part body = MultipartBody.Part.createFormData("file", f.getName(), requestFile); 
       final Call<ResponseBody> resultCall = service.uploadImage(body); 
       resultCall.execute().body(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onCancelled() { 
     super.onCancelled(); 
     if(weakReference != null) 
      weakReference.clear(); 
     weakReference = null; 

     if (progressDialog != null && progressDialog.isShowing()) 
      progressDialog.dismiss(); 
     progressDialog = null; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
     if(weakReference != null) 
      weakReference.clear(); 
     weakReference = null; 

     if (progressDialog != null && progressDialog.isShowing()) 
      progressDialog.dismiss(); 
     progressDialog = null; 
    } 
}