0

在运行“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();
}
4

2 回答 2

2

我的建议是调用runmods(activity)一个新的Runnable,它在延迟后运行。即在 的onShow方法中alertDialog2,使用如下代码:

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

所以完整的修改代码将是:

public static final int DIALOG2_DELAY_SECS = 4;

public static void execute(final MainActivity activity) {
    Rooted.rooted(activity);
    final AlertDialog alertDialog2 = new AlertDialog.Builder(this).create();
    alertDialog2.setTitle("Executing");
    alertDialog2.setMessage("Your phone will now reboot");
    alertDialog2.setOnShowListener(new AlertDialog.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            new Handler().postDelayed(new Runnable(){
                @Override
                public void run() {
                    runmods(activity);
                }
            }, DIALOG2_DELAY_SECS*1000);
        }
    });
    AlertDialog alertDialog = new AlertDialog.Builder(activity).create();
    alertDialog.setTitle("Confirm");
    alertDialog.setMessage("Are you sure you want to execute and reboot?");
    alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            alertDialog2.show();
        }
    });
    alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    alertDialog.show();
}
于 2013-09-10T09:42:48.847 回答
0

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

于 2013-09-09T23:57:16.267 回答