我有一些特定的逻辑,其中需要等待 5 秒才能关闭微调器并显示对话框消息。一切正常,但未显示对话框消息。如果我毫不拖延地做同样的事情,它就会起作用。我的代码是:
public static void showMessageNotSentDialog(Activity inActivity)
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(inActivity);
alertDialogBuilder.setTitle(R.string.error);
alertDialogBuilder.setMessage(R.string.error_sending_message);
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton(R.string.ok,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
public static void showDelayedErrorMessage(final Activity inActivity)
{
Runnable task = new Runnable() {
public void run() {
com.test.classes.Spinner.hideSpinner();
showMessageNotSentDialog(inActivity);
}
worker.schedule(task, com.test.classes.Spinner.TEXT_SPINNER_HIDEOUT_SEC, TimeUnit.SECONDS);
}
尝试从 UI 线程显示对话框:
public static void showDelayedErrorMessage(final Activity inActivity)
{
Runnable task = new Runnable() {
public void run() {
com.test.classes.Spinner.hideSpinner();
Runnable messageTask = new Runnable() {
public void run() {
showMessageNotSentDialog(inActivity);
}
};
inActivity.runOnUiThread(messageTask);
}
};
worker.schedule(task, com.test.classes.Spinner.TEXT_SPINNER_HIDEOUT_SEC, TimeUnit.SECONDS);
}