Android ProgressDialog不';不要在新线程之前启动
我尝试将文件加载到服务器。我需要等待,然后才能对start-enother方法执行所有操作。所以我使用同步调用。我尝试在开始新线程之前显示ProgressDialog,但是,虽然所有线程都没有完成,但我的UI线程只是卡住了Android ProgressDialog不';不要在新线程之前启动,android,progress-bar,Android,Progress Bar,我尝试将文件加载到服务器。我需要等待,然后才能对start-enother方法执行所有操作。所以我使用同步调用。我尝试在开始新线程之前显示ProgressDialog,但是,虽然所有线程都没有完成,但我的UI线程只是卡住了 private void uploadImageSync() { final ProgressDialog progressDialog; progressDialog = new ProgressDialog(BarcodActivity.this);
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();
}
private void uploadImageSync(){
最终进度对话;
progressDialog=新建progressDialog(BarcodActivity.this);
progressDialog.setMessage(“加载…”);
progressDialog.show();
改装改装=新改装.Builder()
.baseUrl(根URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface服务=reformation.create(RequestInterface.class);
int i=0;
(i++您的UI线程在t.join()
行中被阻塞,如上所述:
join方法允许一个线程等待另一个线程的完成
只需调用start
方法就足以运行您的线程。UI线程冻结的原因是您正在调用t.join()
。您的UI线程
等待新的线程完成
相反,您可以使用AsyncTask
,因为该类是为此类任务创建的
下面是一个关于如何使用它和
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
int count=url.length;
长totalSize=0;
for(int i=0;i
请注意,onPostExecute
在UI线程上运行,因此您可以很容易地将其删除。私有类DownloadFileTask扩展了AsyncTask{
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;
}
}
private ProgressDialog ProgressDialog=null;
私有WeakReference WeakReference=null;
公共下载文件任务(活动){
参考=新的WeakReference(活动);
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
progressDialog=新建progressDialog(weakReference.get());
progressDialog.setMessage(“加载…”);
progressDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
改装改装=新改装.Builder()
.baseUrl(根URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface服务=reformation.create(RequestInterface.class);
int i=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;
}
}