我正在使用下面的代码在黑莓应用程序中创建请等待弹出窗口。我想在设备后按时删除该弹出屏幕,但我无法这样做,因为在显示时请等待弹出窗口整个屏幕被阻止直到完成的线程操作。
这是我的代码:
public class PleaseWaitLoginPopupScreen extends PopupScreen {
//statics ------------------------------------------------------------------
private AnimatedGIFField _ourAnimation = null;
private LabelField _ourLabelField = null;
private static String pleaseWaitText="";
private static PleaseWaitLoginPopupScreen ref;
public static PleaseWaitLoginPopupScreen getInstance(){
if(ref!=null){
ref=new PleaseWaitLoginPopupScreen(Constant.PLEASE_WAIT_TEXT);
}
return ref;
}
public PleaseWaitLoginPopupScreen(String text) {
super(new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL | VerticalFieldManager.VERTICAL_SCROLLBAR));
GIFEncodedImage ourAnimation = (GIFEncodedImage) GIFEncodedImage.getEncodedImageResource("loader.gif");
_ourAnimation = new AnimatedGIFField(ourAnimation, Field.FIELD_HCENTER);
this.add(_ourAnimation);
_ourLabelField = new LabelField(text, Field.FIELD_HCENTER);
this.add(_ourLabelField);
}
public static void showScreenAndWait(final Runnable runThis, String text) {
pleaseWaitText=text;
final PleaseWaitLoginPopupScreen thisScreen = new PleaseWaitLoginPopupScreen(text);
Thread threadToRun = new Thread() {
public void run() {
// First, display this screen
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
UiApplication.getUiApplication().pushScreen(thisScreen);
}
});
boolean exceptionFlag = false;
// Now run the code that must be executed in the Background
try {
runThis.run();
} catch (Throwable t) {
exceptionFlag = true;
t.printStackTrace();
//throw new RuntimeException("Exception detected while waiting: " + t.toString());
}finally{
// Now dismiss this screen
if(exceptionFlag){//IF EXCEPTION OCURES THAN THIS CODE WILL RUN TO STOP THE PLEASE WAIT POP TASK
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
UiApplication.getUiApplication().popScreen(thisScreen);
}
});
}
}
}
};
threadToRun.start();
}
public void dismissPopupScreen(){
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
UiApplication.getUiApplication().popScreen(PleaseWaitLoginPopupScreen.this);
}
});
/*synchronized (UiApplication.getEventLock()) {
UiApplication.getUiApplication().popScreen(PleaseWaitLoginPopupScreen.this);
}*/
}
}