如何在BlackBerry中控制此应用程序的空闲时间?

时间:2014-02-04 14:10:57

标签: blackberry java-me

我需要在用户登录时控制应用的空闲时间。

这是我的用例:

  

当空闲时间达到4分钟时,应该有一个选项对话框   显示时可以选择关闭它。

     

如果用户关闭对话框,对话框就会关闭   ELSE
  空闲时间的计时器应该继续,当它达到5分钟(1分钟后)时,必须关闭对话框。

     

新消息对话框必须显示5秒钟   5秒后,必须关闭新对话框。

     

我必须执行一些已经注销用户的代码。

实际上,我有一个有效的代码,但它有点模糊。

要知道空闲时间,我这样做: DeviceInfo.getIdleTime()

该方法由 RealtimeClockListener 控制,因此当用户登录时,我会执行 UiApplication.getUiApplication()。addRealtimeClockListener ,当它注销时我只是这样做 UiApplication.getUiApplication()。removeRealtimeClockListener

如果需要,我可以发布我的代码,但我真的希望看到有人可以建议的新方法。

更新

这是我正在使用的解决方案。

public static RealtimeClockListener realtimeClockListener = new RealtimeClockListener() {
        int _4Minutes = 60 * 4;
        int _5Minutes = 60 * 5;
        int _5Seconds = 5 * 1000;
        Dialog dialog4Minutes = null;
        Dialog dialog5Minutes = null;
        Timer timer5Seconds = null;
        TimerTask timerTask5Seconds = null;
        public void clockUpdated() {
            if ( Application.getApplication().isForeground() ) { 
                appInactiveTime = (int) DeviceInfo.getIdleTime(); 
                inForegroundFlag = true; 
            } else { 
                if ( inForegroundFlag ) { 
                appInactiveTime = 0; 
                } else { 
                    appInactiveTime = appInactiveTime + 60; 
                } 
                inForegroundFlag = false; 
            }
            synchronized (UiApplication.getEventLock()) {
                UiEngine ui = Ui.getUiEngine();
                if ( appInactiveTime < _4Minutes ){
                    if ( dialog4Minutes != null ) {
                        dialog4Minutes.close();
                        dialog4Minutes = null;
                    }
                }
                if ( appInactiveTime < _5Minutes ){
                    if ( dialog5Minutes != null ) {
                        dialog5Minutes.close();
                        dialog5Minutes = null;
                    }
                }
                if ( appInactiveTime >= _4Minutes && appInactiveTime < _5Minutes ) {
                    if ( dialog4Minutes == null ) {
                        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 ( appInactiveTime >=_5Minutes ) {
                    if ( dialog5Minutes == null ) {
                        dialog5Minutes = new Dialog("You will be disconnected", new String[] {"OK"}, new int[]{1}, 1, null);
                        ui.pushGlobalScreen(dialog5Minutes, 1,UiEngine.GLOBAL_QUEUE);
                        timerTask5Seconds = new TimerTask() {

                            public void run() {
                                UiApplication.getUiApplication().invokeLater(new Runnable() {

                                    public void run() {
                                        dialog5Minutes.close();
                                        try {
                                            //logout in communication manager 
                                            //pop to initial screen
                                        } catch (Exception e) {
                                            //force logout (2nd way)
                                        }finally{
                                            UiApplication.getUiApplication().removeRealtimeClockListener(realtimeClockListener);
                                        }
                                        timerTask5Seconds.cancel();
                                        timerTask5Seconds = null;

                                        timer5Seconds.cancel();
                                        timer5Seconds = null;                                       
                                    }
                                });
                            }
                        };
                        timer5Seconds = new Timer();
                        timer5Seconds.schedule(timerTask5Seconds, _5Seconds);
                        if ( dialog4Minutes != null ) {
                            dialog4Minutes.close();
                            dialog4Minutes = null;
                        }
                    }
                }
            }
        }
    };

谢谢Peter Strange!

1 个答案:

答案 0 :(得分:1)

我认为您将使用RealtimeClockListener作为&#39; tick&#39;每分钟唤醒你的应用程序。这是有效的,但确实意味着你的超时时间不会是4分钟 - 它的实际持续时间将取决于“分钟”中的下落。用户变得空闲。

诀窍是确定当滴答发生或更准确时你的应用程序处于什么状态。我认为以下示例代码描述了这一点,假设最初appInactiveTime设置为0且inForegrounFlag为真。

if(Application.getApplication()。isForeground()){ appInactiveTime = DeviceInfo.getIdleTime(); inForegrounFlag = true; } else { if(inForegrounFlag){ //在前台,从现在开始空闲时间 appInactiveTime = 0; } else { appInactiveTime = appInactiveTime + 60; } inForegrounFlag = false; }

我相信使用此代码,现在可以使用appInactiveTime通过将应用程序与240(60 * 4)进行比较来确定应用程序是否处于非活动状态超过4分钟。 。

此时,您的应用程序应创建并显示全局屏幕,如果您希望它显示,无论该应用程序是否在前景中。然后在下一个勾选中,您可以决定是否要关闭此屏幕,或者用户是否已将其解除。我觉得你需要时间方面的帮助,而不是处理的这一部分,所以我会留下这个。