删除请等待后退键上的弹出屏幕

时间:2013-03-14 13:03:50

标签: blackberry

我正在使用以下代码创建请在Blackberry应用程序中等待弹出窗口。我想在设备的后退上删除该弹出屏幕,但我无法执行此操作,因为 在显示时请等待弹出,整个屏幕被阻止,直到完成线程操作。

这是我的代码:

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);
        }*/
    }
}

1 个答案:

答案 0 :(得分:3)

如果您想要按后退(ESC)键,并使用它来关闭弹出屏幕,那么您可以覆盖PleaseWaitLoginPopupScreen中的keyChar(char,int,int)方法类:

   protected boolean keyChar(char c, int status, int time) {
      if (c == Characters.ESCAPE) {
         close();
      }
      return super.keyChar(c, status, time);
   }

然而,所有这一切都只是删除弹出屏幕。您可能还应该尝试停止您开始的Runnable。在BlackBerry Java中,这需要在请求停止的代码和Runnable本身之间协作完成。

See this answer by Arhimed for more on this

在您的情况下,您可以将Thread变量存储为成员

private Thread _threadToRun; 

showScreenAndWait()中分配:

thisScreen._threadToRun = new Thread() {

然后在我用这个显示的keyChar()方法中取消它:

   protected boolean keyChar(char c, int status, int time) {
      if (c == Characters.ESCAPE) {
         _threadToRun.interrupt();
         close();
      }
      return super.keyChar(c, status, time);
   }

然后,在传递给Runnable()的{​​{1}}中,您需要进行一些检查以查看该线程是否已被中断:

showScreenAndWait()

如何放置这些检查取决于任务。如果您使用 if (!Thread.currentThread().isInterrupted()) { // do more stuff } 方法下载10个文件,则可能需要在10次下载之间进行run()检查。如果isInterrupted()包含run()循环,请在每个循环中检查一次。这将决定何时可以停止作业。