对于BlackBerry中的这个用例,我应该如何控制我的应用程序的空闲时间?
当用户登录时,我需要控制我的应用程序的空闲时间 这是我的用例: 当空闲时间达到4分钟时,应显示选项对话框 显示了关闭它的选项 如果用户关闭该对话框,则该对话框只会关闭对于BlackBerry中的这个用例,我应该如何控制我的应用程序的空闲时间?,blackberry,java-me,Blackberry,Java Me,当用户登录时,我需要控制我的应用程序的空闲时间 这是我的用例: 当空闲时间达到4分钟时,应显示选项对话框 显示了关闭它的选项 如果用户关闭该对话框,则该对话框只会关闭 否则 空闲时间的计时器应继续,当达到5分钟(1分钟后)时,对话框必须关闭 新消息对话框必须显示5秒钟。 5秒钟后,新对话框必须关闭 我必须执行一些代码,我已经注销了用户 事实上,我有一个工作代码,但它有点模糊 要了解空闲时间,我需要执行以下操作:DeviceInfo.getIdleTime() 该方法由RealtimeClockL
否则
空闲时间的计时器应继续,当达到5分钟(1分钟后)时,对话框必须关闭 新消息对话框必须显示5秒钟。
5秒钟后,新对话框必须关闭 我必须执行一些代码,我已经注销了用户 事实上,我有一个工作代码,但它有点模糊 要了解空闲时间,我需要执行以下操作:DeviceInfo.getIdleTime() 该方法由RealtimeClockListener控制,因此当用户登录时,我执行UiApplication.getUiApplication().addRealtimeClockListener,当用户注销时,我只执行UiApplication.getUiApplication().addRealtimeClockListener UiApplication.getUiApplication().RemoveEalTimeClockListener 如果需要的话,我可以发布我的代码,但我真的很想看到有人可以建议的新方法 更新 这就是我正在使用的解决方案
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;
}
}
}
}
}
};
公共静态RealtimeClockListener RealtimeClockListener=new RealtimeClockListener(){
int _4分钟=60*4;
int _5分钟=60*5;
int_5秒=5*1000;
对话框4分钟=空;
对话框5分钟=空;
计时器timer5Seconds=null;
TimerTask timerTask5Seconds=null;
已更新的公共文件(){
如果(Application.getApplication().is前台()){
appInactiveTime=(int)DeviceInfo.getIdleTime();
inForegroundFlag=true;
}否则{
if(inForegroundFlag){
appInactiveTime=0;
}否则{
appInactiveTime=appInactiveTime+60;
}
inForegroundFlag=false;
}
已同步(UiApplication.getEventLock()){
UiEngine ui=ui.getUiEngine();
如果(应用不活动时间<_4分钟){
if(dialog4Minutes!=null){
dialog4Minutes.close();
dialog4Minutes=null;
}
}
如果(应用不活动时间<_5分钟){
如果(对话框5分钟!=null){
对话框5分钟。关闭();
dialog5Minutes=null;
}
}
如果(appInactiveTime>=\u 4分钟和&appInactiveTime<\u 5分钟){
如果(dialog4Minutes==null){
dialog4Minutes=newdialog(“保持登录?”,新字符串[]{“SI”,“NO”},新int[]{1,2},2,null);
pushGlobalScreen(对话框4分钟,1,UiEngine.GLOBAL_队列);
}
}否则如果(应用不活动时间>=\u 5分钟){
如果(dialog5Minutes==null){
dialog5Minutes=newdialog(“您将被断开连接”,新字符串[]{“OK”},新int[]{1},1,null);
ui.pushGlobalScreen(对话框5分钟,1,UiEngine.GLOBAL_队列);
timerTask5Seconds=新的TimerTask(){
公开募捐{
UiApplication.getUiApplication().invokeLater(新的Runnable()){
公开募捐{
对话框5分钟。关闭();
试一试{
//在通讯管理器中注销
//弹出到初始屏幕
}捕获(例外e){
//强制注销(第二种方式)
}最后{
UiApplication.getUiApplication().RemoveEalTimeClockListener(realtimeClockListener);
}
timerTask5秒。取消();
timerTask5Seconds=null;
timer5s.cancel();
timer5Seconds=null;
}
});
}
};
timer5Seconds=新计时器();
timer5Seconds.时间表(timerTask5Seconds,_5Seconds);
if(dialog4Minutes!=null){
dialog4Minutes.close();
dialog4Minutes=null;
}
}
}
}
}
};
谢谢你,彼得·斯特兰奇 我假设您将使用RealtimeClockListener作为每分钟唤醒应用程序的“滴答声”。这是有效的,但确实意味着你的超时时间不会精确到4分钟-它的实际持续时间将取决于用户空闲的“分钟”中的位置 诀窍是确定当滴答声发生时应用程序处于什么状态,或者更准确地说。我认为下面的示例代码描述了这一点,假设最初的AppInactivateTime设置为0,inForegrounFlag为true if(Application.getApplication().is前台()){ appInactiveTime=DeviceInfo.getIdleTime(); inForegrounFlag=true; }否则{ if(inForegrounFlag){ //在前台,从现在开始空闲计时 appInactiveTime=0; }否则{ appInactiveTime=appInactiveTime+60; } inForegrounFlag=false; } 我相信使用这段代码