2016-11-29 118 views
0

一天的美好时光。
我想让应用程序上传文件到服务器,并显示在ProgressDialog上传的进度。更新进度对话框

这是我的代码至今:
的上传过程创建ProgressDialog

private void createUploadingDialog(int filesCount) { 
    uploadingDialog = new ProgressDialog(getActivity()); 
    uploadingDialog.setIndeterminate(true); 
    loadingDialog.setMessage(getString(R.string.upload_progress, 0, filesCount)); 
    loadingDialog.show(); 
} 

辞退ProgressDialog对上传程序

private void dismissUploadingDialog() { 
    if (uploadingDialog.isShowing()) 
     uploadingDialog.dismiss(); 
} 

更新ProgressDialog对上传程序

private void updateUploadingDialog(final String dialogMessage) { 
    class UpdateDialog implements Runnable { 
     private String messageString; 
     private UpdateDialog(String ms) { messageString = ms; } 
     public void run() { 
      uploadingDialog.setMessage(messageString); 
     } 
    } 
    getActivity().runOnUiThread(new UpdateDialog(dialogMessage)); 
} 

呼叫建立对话

createUploadingDialog(imagesPathsArray.size()); 

呼叫驳回对话框

dismissUploadingDialog(); 

呼叫更新对话框的onSuccess

updateUploadingDialog(getString(R.string.upload_progress, i, imagesPathsArray.size())); 

呼叫更新对话框onFailure处

updateUploadingDialog(getString(R.string.retrying_request)); 

显示在对话框中的片段。要将图像发送到服务器,我使用了Retrofit2。

当我做对话框初始设置,消息得到正确设置(显示总文件和当前一个),但是,更新调用时消息不更新。出于某种原因,没有错误抛出,如果我加入Log.d,它甚至表明该方法被调用。

谢谢你的帮助!

+0

您应该阅读此。这将帮助您。 http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog?rq=1 –

回答

0

我想你在呼唤更新进度只有一次,你怎么listning进步状态改变监听器?如果你想使用,你必须不断地打电话updateUploadingDialog这是不是一个很好的程序

你必须写上载过程中的AsyncTask和onProgressUpdate更新进度()方法

Herehere是例如用于对preExecute参考

初始化进度条,在doinbackground上传文件时,在onProgressUpdate更新进度BA r和对postexecute你想上传后做

希望这按不使用的AsyncTask帮助

0

什么都,你不能得到onProgressUpdate功能的机会。所以,你必须手动更新你的进度条状态,就像使用自己的线程一样,你已经做到了。但是,我使用下面的源代码解决了我的问题,您可以检查它:

private void createThread(){ 
    Runnable runable = new updateProgressStatus(); 
    progressUpdateThread = new Thread(runnable); 
    progressUpdateThread.start(); 
} 

public class updateProgressStatus implements Runnable { 
    public void run() { 
     while(Thread.currentThread()==progressUpdateThread)    
      try { 
       Thread.sleep(1000);  
       progressHandler.sendMessage(YOUR MESSAGE); 
      } catch (InterruptedException e) { 
       // Interrupt problem 
       Thread.currentThread().interrupt(); 
      } catch (Exception e) { 
     e.printStackTrace(); 
     } 
    } 
} 

private Handler progressHandler = new Handler(){ 
    @Override 
    public void handleMessage(Message message) { 
     // Now, update your progress bar status 
     progress.setProgress(message.what); 
    } 
};