[英]How to close a global dialog and display another in 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秒后如何關閉第二個對話框? 我當時正在考慮使用計時器,但我不知道這是否是最佳方法。
提前致謝!
我認為您的代碼中存在一個簡單的編碼錯誤,這可能是導致您的問題的原因。 每次您通過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;
}
}
}
}
}
};
我不確定是否需要“同步(UiApplication.getEventLock())”,因為我認為clockUpdated在事件線程上運行,但是在此代碼中,它不會受到影響。
關於另一個問題,關於如何處理5秒后的關閉,請查看Timer類。 如果用戶確實承認Dialog,則必須編寫setDialogClosedListener(在示例中已被我忽略)的代碼才能采取措施。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.