如何在 ProgressDialog 关闭后立即执行指令/方法? [英] How to execute an instruction/method immediately after ProgressDialog dismisses?
问题描述
我正在尝试执行方法 doSomeWork();
在 ProgressDialog 在我的方法 printing();
中解散后,该方法似乎与其他方法和对话框没有出现.如果我评论方法 doSomeWork();
对话框会正确显示,直到线程完成.
I am trying to execute the method doSomeWork();
after the ProgressDialog dismisses in my method printing();
which seems to be overlapped by the other method and the dialog is not showed up. If I comment method doSomeWork();
the dialog is displayed correctly until the thread is finished.
这是我的方法 printing();
public void printing()
{
final ProgressDialog printingDialog = ProgressDialog.show(this, "Printing...", "Please wait", true, false);
new Thread(new Runnable() {
@Override
public void run()
{
//something big executing here
}
}).start();
}
他是我的方法doSomework()
:
public void doSomeWork(){
Thread receiptPrint = new Thread(new Runnable() {
@Override
public void run() {
//something here
runOnUiThread(new Runnable() {
@Override
public void run() {
//another dialog here
}
});
}
});
}
在这里你可以看到我是如何调用这两个方法的:
Here you can see the how I am calling those two methods:
private OnClickListener onClickPrint = new OnClickListener() {
public void onClick(final View v) {
Log.d("Button","Clicked on Print Order Button");
printing();
doSomeWork();
有谁知道我怎样才能在 printing();
完全完成时才执行 doSomeWork()
?
Does anyone know how could I execute doSomeWork()
only when printing();
will be completely finished?
推荐答案
这是 AsyncTask
的目的之一.它看起来类似于:
This is one of the purposes of an AsyncTask
. It would look similar to this:
public void onClick(final View v) {
new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
//Show your progress dialog in here
super.onPreExecute();
}
@Override
protected Void doInBackground( Void... params ) {
printing();
return null;
}
@Override
protected void onPostExecute( Void result ) {
//Dismiss your progress dialog here
doSomeWork();
}
}.execute();
}
这篇关于如何在 ProgressDialog 关闭后立即执行指令/方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!