如何关闭全局对话框并在BlackBerry中显示另一个对话框?

时间:2014-02-05 13:40:06

标签: java blackberry

当谜语时间大于或等于4分钟时,我需要显示 消息对话框。如果空闲时间大于或等于5分钟, 我需要关闭第一个对话框并推送另一个应该自动生成的对话框 5秒钟后关闭。

这是我到目前为止所做的:

public static RealtimeClockListener clockListenerTest = new RealtimeClockListener() {
                public void clockUpdated() {
                        int _4Minutes = 60 * 4;
                        int _5Minutes = 60 * 5;
                        Dialog dialog4Minutes = new Dialog("Stay Logged In?", new String[] {"SI", "NO"}, new int[]{1,2}, 2, null);
                        dialog4Minutes.setDialogClosedListener(new DialogClosedListener() {
                                public void dialogClosed(Dialog dialog, int choice) {
                                    //TODO
                                }
                        });
                        Dialog dialog5Minutes = new Dialog("You will be disconnected", new String[] {"OK"}, new int[]{1}, 1, null);
                        dialog5Minutes.setDialogClosedListener(new DialogClosedListener() {
                                public void dialogClosed(Dialog dialog, int choice) {
                                    //TODO
                                }
                        });
                        synchronized (UiApplication.getEventLock()) {
                                UiEngine ui = Ui.getUiEngine();
                                if(DeviceInfo.getIdleTime()>=_4Minutes && DeviceInfo.getIdleTime() < _5Minutes){
                                        ui.pushGlobalScreen(dialog4Minutes, 1,UiEngine.GLOBAL_QUEUE);
                                }else if(DeviceInfo.getIdleTime()>=_5Minutes){
                                        dialog4Minutes.close();
                                        ui.pushGlobalScreen(dialog5Minutes, 1,UiEngine.GLOBAL_QUEUE);
                                }
                        }
                }
        };

我的代码问题是第一个对话框永远不会在else子句中关闭 在手动关闭第一个对话框之前,不会显示第二个对话框。

如何正确使用else子句中的代码? 我应该如何在5秒后关闭第二个对话框?我正在考虑一个计时器 但我不知道这是不是最好的方法。

提前致谢!

1 个答案:

答案 0 :(得分:1)

我认为您的代码中存在一个简单的编码错误,这可能会导致您的问题。每次通过clockUpdated()时,都会创建一个新的dialog4Minutes。因此,当您使用dialog4Minutes.close()关闭它时,您实际上正在关闭刚刚创建的那个,而不是正在显示的那个。这是一些示例(未编译,未测试)的替换代码,它表示我将如何执行此操作:

public static RealtimeClockListener clockListenerTest = new RealtimeClockListener() {
    int _4Minutes = 60 * 4;
    int _5Minutes = 60 * 5;
    Dialog dialog4Minutes = null;
    Dialog dialog5Minutes = null;
    public void clockUpdated() {
        synchronized (UiApplication.getEventLock()) {
            UiEngine ui = Ui.getUiEngine();
            if ( DeviceInfo.getIdleTime() < _4Minutes ){
                // Shouldn't be needed - this is just for safety
                if ( dialog4Minutes != null ) {
                    dialog4Minutes.close();
                    dialog4Minutes = null;
                }
            }
            if ( DeviceInfo.getIdleTime() < _5Minutes ){
                // Shouldn't be needed - this is just for safety
                if ( dialog5Minutes != null ) {
                    dialog5Minutes.close();
                    dialog5Minutes = null;
                }
            }
            if ( DeviceInfo.getIdleTime() >= _4Minutes && DeviceInfo.getIdleTime() < _5Minutes ) {
                if ( dialog4Minutes == null ) {
                    // 4 minute Dialog has not been pushed yet
                    dialog4Minutes = new Dialog("Stay Logged In?", new String[] {"SI", "NO"}, new int[]{1,2}, 2, null);
                    ui.pushGlobalScreen(dialog4Minutes, 1,UiEngine.GLOBAL_QUEUE);
                }
            } else if ( DeviceInfo.getIdleTime()>=_5Minutes ) {
                if ( dialog5Minutes == null ) {
                    // 5 minute Dialog has not been pushed yet
                    dialog5Minutes = new Dialog("You will be disconnected", new String[] {"OK"}, new int[]{1}, 1, null);
                    ui.pushGlobalScreen(dialog5Minutes, 1,UiEngine.GLOBAL_QUEUE);
                    if ( dialog4Minutes != null ) {
                        dialog4Minutes.close();
                        dialog4Minutes = null;
                    }
                }
            }
        }
    }
};

我不确定是否需要“synchronized(UiApplication.getEventLock())”,因为我认为clockUpdated在事件线程上运行,但在此代码中,它不会受到伤害。

关于你的另一个问题,关于如何在5秒后处理关闭,请查看Timer类。您将不得不编写setDialogClosedListener(我在我的示例中已忽略)的代码,以便在用户确实确认Dialog时执行操作。