在Blackberry中经过一段时间后关闭应用程序

时间:2012-08-07 11:41:17

标签: session blackberry

我希望在一段时间后关闭后台应用程序。      基本上我希望维持用户会话。会话可以持续5到10分钟。

1 个答案:

答案 0 :(得分:6)

// to close the application after some time if it is in background.
// you have to override the following method. Pass the time in seconds 
// after which you want to close the application.

    public void deactivate(){       
        new CloseAppInBackground(time duration);        
    }


    public class CloseAppInBackground {
        Timer timer;

        public CloseAppInBackground(int seconds) {
            timer = new Timer();
            timer.schedule(new CloseBackgroundAppTask(), seconds*1000);
        }

        class CloseBackgroundAppTask extends TimerTask {
            public void run() {
                // check whether the application is in foreground or not 
                if(!UiApplication.getUiApplication().isForeground())
                    System.exit(0); // exit the application.

                    timer.cancel(); //Terminate the timer thread
                 }
        }
    }