消息处理程序问题

时间:2012-04-22 17:31:23

标签: android android-widget

我正在使用消息和处理程序来更改执行线程时显示的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();

}

我现在遇到的问题是在完成操作之前发送endmessageProcessDialog以0.5秒的速度关闭。如果所有Boolean都为真,则会在msg1, msg2, msg3, emdmsg中快速显示System.out并关闭。为什么会这样?我该如何解决?

2 个答案:

答案 0 :(得分: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;
    }

最后一次休息不是严格需要的,但是总是以休息结束它们是一个好习惯,所以如果你以后添加一个新案例,你不必记得在前一个案例中添加一个中断。

答案 1 :(得分:0)

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

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

这是你的问题吗?