How to close a global dialog and display another i

2020-08-05 11:22发布

问题:

When the iddle time is greater than or equal to 4 minutes, I need to display a message dialog. If the idle time is greater than or equals to 5 minutes, I need to close the first dialog and push another dialog that should be automatically closed after 5 seconds.

This is what I have so far:

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);
                                }
                        }
                }
        };

Problem with my code is that the first dialog is never closed in the else clause and the second dialog is not displayed until the first dialog is manually closed.

How can I make to work properly the code inside the else clause? And how should I close the second dialog after 5 seconds? I was thinking about a timer for that but I don't know if it's the best way.

Thanks in advance!

回答1:

I think there is a simple coding error in your code, which may be contributing to your problem. Every time you go through clockUpdated() you create a new dialog4Minutes. So when you go to close it, with dialog4Minutes.close(), you are actually closing the one your have just created, not the one that is being displayed. Here is some sample (not compiled, not tested) replacement code which indicates how I would do this:

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;
                    }
                }
            }
        }
    }
};

I am not sure the "synchronized (UiApplication.getEventLock())" is needed since I think clockUpdated runs on the Event Thread, but in this code, it won't hurt.

Regarding your other question, about how to handle the close after 5 seconds, look at the Timer class. You are going to have to code the setDialogClosedListener (which I have ignored in my sample) to take action if the user does in fact acknowledge the Dialog.