[英]Android ProgressDialog not showing (blocked by code in other Thread)
Looking to similar questions everybody solve the problem of not appearing their Progress Dialog putting the intermediate code in a separate Thread. 看着类似的问题,每个人都解决了不出现将中间代码放在单独的线程中的“进度对话框”的问题。
My problem is that the mentioned solution is not working for me. 我的问题是上述解决方案对我不起作用。
In my activity: 在我的活动中:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.dialog_ddbb_download_text)
.setPositiveButton(R.string.Accept, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// In this method I show the progress dialog
showProgressAndDownloadDDBB();
}
})
.setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
builder.create().show();
Method in the activity: 活动中的方法:
private void showProgressAndDownloadDDBB() {
progressDialog = new ProgressDialog(mContext);
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.show();
// Here I call the Runnable to execute the code in other Thread and let the UI draw the Progress Dialog. If it wasn't called, the progress dialog does appear.
DDBB_Download_Manager ddbb_download_manager = new DDBB_Download_Manager(mContext, progressDialog);
ddbb_download_manager.run();
}
My runnable class, expected to run the intermediate code in a separate Thread: 我的可运行类,期望在单独的线程中运行中间代码:
public class DDBB_Download_Manager implements Runnable {
public DDBB_Download_Manager(Context context, ProgressDialog progressDialog) {
this.mContext = context;
this.mProgresDialog = progressDialog;
}
@Override
public void run() {
someCode()
Thread.sleep(3000);
// The GUI shows the accept Button clicked for 3 seconds (like it was freezed)
// Here I try to hide the Progress dialog after finishing the job, but it doesn't matter becasuse the progress dialog didn't even show up.
View rootView = ((Activity)mContext).getWindow().getDecorView().findViewById(android.R.id.content);
rootView.post(new Runnable() {
@Override
public void run() {
mProgresDialog.dismiss();
}
});
}
So the question is: if I am executing the code between the Show and Dismiss methods of the Progress Dialog in a different Thread than the UI Thread, why is not the dialog showing up? 所以问题是:如果我在与UI线程不同的线程中执行“进度对话框”的“显示”和“关闭”方法之间的代码,为什么没有显示该对话框?
In fact it appears If I don't call the Runnable.
That is because you are running directly the dismiss()
method from the runnable when you call ddbb_download_manager.run()
where the progress dialog is cleared/done and if you are not calling it then the progress dialog will show due to that dismiss is on yet been called. 那是因为当您调用
ddbb_download_manager.run()
,您正在运行的可运行对象中直接运行dismiss()
方法,在该对话框中清除/完成了进度对话框;如果您不调用它,则由于关闭了ddbb_download_manager.run()
,将显示进度对话框尚未被召集。
Make sure that you call ddbb_download_manager.run()
when you want your progress dialog to be dismissed. 如果要关闭进度对话框,请确保调用
ddbb_download_manager.run()
。 don't call it directly after you show your progress dialog. 显示进度对话框后,请勿直接调用它。
private void showProgressAndDownloadDDBB() {
progressDialog = new ProgressDialog(mContext);
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.show();
// Here I call the Runnable to execute the code in other Thread and let the UI draw the Progress Dialog. If it wasn't called, the progress dialog does appear.
DDBB_Download_Manager ddbb_download_manager = new DDBB_Download_Manager(mContext, progressDialog);
Handler handler = new Handler();
handler.postDelayed(ddbb_download_manager ,3*1000);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.