问题
This is the handler, in the main Thread, which shows and dismisses a progress dialog.
public static final int SHOW = 0;
public static final int DISMISS = 1;
public Handler pdHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.i(TAG, "+ handleMessage(msg:" + msg + ")");
switch(msg.what) {
case SHOW:
pd = ProgressDialog.show(LogViewer.this, "", getText(R.string.loading_msg), true);
break;
case DISMISS:
if(pd != null) {
pd.dismiss();
pd = null;
}
break;
}
}
};
The message to show the progress is:
pdHandler.sendMessage(pdHandler.obtainMessage(SHOW));
The message to dismiss it is:
pdHandler.sendMessage(pdHandler.obtainMessage(DISMISS));
It works well when I call it before I start an AsyncTask, the AsyncTask onPostExecute() dismisses it.
But when it runs within a runnable (runOnUiThread), the dialog does not show. Examining the pd variable on the debugger, it shows that is is created, it is running and it is visible, but in practice it is not visible.
Any suggestion?
UPDATE:
I did the obvious test I should have done in the first place. I commented the DISMISS message. And the progress dialog did show up. It appeared too late, after the runnable was finished.
I undestand now that the DISMISS message did dismiss the not-yet-visible ProgressDialog, that's why I did not see it.
The question becomes now: I need the ProgressDialog to show BEFORE the runnable code is executed. And it is not so straight forward. My call hierarchy is like this:
onScrollEventChanged
--> runOnUiThread (
--> checkScrollLimits
--> if need to scroll
show ProgressDialog "Loading"
get new data into table
dismiss ProgressDIalog
)
I tried something like this:
onScrollEventChanged
--> checkScrollLimits
--> if need to scroll
show ProgressDialog "Loading"
--> runOnUiThread (
get new data into table
dismiss ProgressDIalog
)
But still the dismiss message got there before the ProgressDialog could show.
According to Logcat there is a five second interval between the arrival of the SHOW message and the arrival of the DISMISS message.
UPDATE II:
I though I will use the isShowing() method of ProgressDIalog
pd = ProgressDialog.show(...)
while(!pd.isShowing());
But it does not help at all, it returns true even if the dialog is not showing yet.
回答1:
Maybe you should NOT use runOnUiThread()
and better handle this via the Looper.prepare()
function:
What is the purpose of Looper and how to use it?
Furthermore your AsyncTask
is finished and runs in onPostExecute()
then you are in the UI-Thread so an additional runOnUiThread()
is not necessary.
回答2:
I Think it will not work.The Place from where you are sending message
pdHandler.sendMessage(pdHandler.obtainMessage(DISMISS));
you need to put all those heavy statements or work into another non ui thread. Handler is used to put a message into Thread message que if your thread will busy then it will execute that message after previous messsage. So because of your ui heavy work that dialog box is not updating. Always use services for your background work. Never try to put everything in your UI thread only.
Hope it will help you otherwise use AsyncTask it is the best method to do such things. If still you are facing the same problem then post that code from where you are sending your message to handler.
回答3:
I made it work by turning the code into AsyncTask and using try/catch blocks.
See my answer in this post
I am not sure if AsyncTask is necessary, but after I had it, the try/catch block made the difference.
来源:https://stackoverflow.com/questions/10935400/android-progress-dialog-not-showing