-1

我正在使用消息和处理程序来更改在执行线程时显示的 processdialog 的消息。代码如下。

final ProgressDialog processdialog = ProgressDialog
        .show(MultiBootManager.this, EMPTY,
                EMPTY, true);
final Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.arg1) {
        case 0:
            System.out.println("msg0");
            processdialog
                    .setMessage(getString(R.string.formating)
                            + folderpath
                            + getString(R.string.cachext3));
        case 1:
            System.out.println("msg1");
            processdialog
                    .setMessage(getString(R.string.formating)
                            + folderpath
                            + getString(R.string.dataext3));
        case 2:
            System.out.println("msg2");
            processdialog
                    .setMessage(getString(R.string.formating)
                            + folderpath
                            + getString(R.string.systemext3));
        case 3:
            System.out.println("endmsg");
            processdialog.dismiss();

        }
    }
};
Thread checkUpdate = new Thread() {
    @Override
    public void run() {
        String[] shellinput = { EMPTY,
                EMPTY, EMPTY, EMPTY, EMPTY };
        shellinput[0] = CMD_MKE2FS_EXT3;
        shellinput[1] = folderpath;
        if (cacheCheckBool) {
            final Message m0 = new Message();
            m0.arg1 = 0;
            handler.sendMessage(m0);
            shellinput[2] = CACHE_IMG;
            processManager
                    .inputStreamReader(
                            shellinput, 20);
        }
        if (dataCheckBool) {
            final Message m1 = new Message();
            m1.arg1 = 1;
            handler.sendMessage(m1);
            shellinput[2] = DATA_IMG;
            processManager
                    .inputStreamReader(
                            shellinput, 20);
        }
        final Message endmessage;
        if (systemCheckBool) {
            final Message m2 = new Message();
            m2.arg1 = 2;
            handler.sendMessage(m2);
            shellinput[2] = DATA_IMG;
            processManager
                    .inputStreamReader(
                            shellinput, 20);
        }
        endmessage = new Message();
        endmessage.arg1 = 3;
        handler.sendMessage(endmessage);
    }
};
checkUpdate.start();

}

我现在遇到的问题是endmessage在操作完成之前发送,ProcessDialog在 0.5 秒内关闭。如果一切Boolean都是真的,它会msg1, msg2, msg3, emdmsg迅速显示System.out并关闭。为什么会有这种行为?我如何解决它?

4

2 回答 2

1

您需要为每个案例添加中断。否则,将执行到下一个案例。

public void handleMessage(Message msg) {
    switch (msg.arg1) {
    case 0:
        System.out.println("msg0");
        processdialog
                .setMessage(getString(R.string.formating)
                        + folderpath
                        + getString(R.string.cachext3));
        break;
    case 1:
        System.out.println("msg1");
        processdialog
                .setMessage(getString(R.string.formating)
                        + folderpath
                        + getString(R.string.dataext3));
        break;
    case 2:
        System.out.println("msg2");
        processdialog
                .setMessage(getString(R.string.formating)
                        + folderpath
                        + getString(R.string.systemext3));
        break;
    case 3:
        System.out.println("endmsg");
        processdialog.dismiss();
        break;
    }

最后一个 break 并不是绝对需要的,但总是以 break 结束它们是一个好习惯,这样如果你以后添加一个新案例,你就不必记得在前一个案例中添加一个 break。

于 2012-04-22T17:40:05.967 回答
0

我不确定我是否理解 - 但你的代码只是“触发”sendMessages而没有任何命令。因此,特别是它会遍历每个 if 语句,触发相关的消息,然后 - 无论这些消息或其他任何地方发生什么,它都会到达handler.sendMessage(endmessage); 并且 justz 也会触发它。

现在我看到一个“看门人”检查何时发送您的 endMsg。

这是你的问题吗?

于 2012-04-22T17:38:42.067 回答