onClick任务不等待在运行任务之前显示AlertDialog

时间:2013-09-09 22:37:31

标签: java android

我在运行“runmods(activity);”之前尝试让alertdialog2显示时遇到问题。

按下按钮后,它不会显示alertdialog2并立即运行“runmods”

我想要的是能够按“执行”,然后 alertdialog2显示,然后 runmods运行。

public static void execute(final MainActivity activity) {
    Rooted.rooted(activity);
    AlertDialog alertDialog = new AlertDialog.Builder(activity).create();
    final AlertDialog alertDialog2 = new AlertDialog.Builder(activity).create();
    alertDialog.setTitle("Confirm");
    alertDialog.setMessage("Are you sure you want to execute and reboot?");
    alertDialog2.setTitle("Executing");
    alertDialog2.setMessage("Your phone will now reboot");

    alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            alertDialog2.setOnShowListener(new AlertDialog.OnShowListener() {
                @Override
                public void onShow(DialogInterface dialog) {

                    runmods(activity);
                }
            });
            alertDialog2.show();
        }
    });
    alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    alertDialog.show();
}

2 个答案:

答案 0 :(得分:2)

我的建议是在延迟后运行的新runmods(activity)中调用Runnable。也就是说,在onShow的{​​{1}}方法中,使用以下代码:

alertDialog2

因此完整的修改后的代码将是:

new Handler().postDelayed(new Runnable(){
    @Override
    public void run() {
        runmods(activity);
    }
}, 4000); //alertDialog2 will be displayed for 4 seconds before runmods is called

答案 1 :(得分:0)

如果您希望警报2显示3秒钟,请尝试使用线程并使其在启动runmods之前休眠3000毫秒