1

我正在使用下面的代码在黑莓应用程序中创建请等待弹出窗口。我想在设备后按时删除该弹出屏幕,我无法这样做,因为在显示时请等待弹出窗口整个屏幕被阻止直到完成的线程操作。

这是我的代码:

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

1 回答 1

3

如果你想捕捉返回(ESC) 按键,并使用它来关闭弹出屏幕,那么你可以在你的类中覆盖keyChar(char,int,int)方法PleaseWaitLoginPopupScreen

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

但是,这一切只是简单地删除弹出屏幕。您可能还应该尝试停止Runnable您开始的操作。在 BlackBerry Java 中,这需要在请求停止的代码和Runnable自身之间协作完成。

有关更多信息,请参阅 Arhimed 的此答案

在您的情况下,您可以将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()您传递给的 中showScreenAndWait(),您需要进行一些检查以查看线程是否已被中断:

 if (!Thread.currentThread().isInterrupted()) {
     // do more stuff
 }

如何放置这些检查取决于任务。如果您在您的run()方法中下载 10 个文件,您可能应该isInterrupted()在 10 个下载之间进行检查。如果run()包含while()循环,则每个循环检查一次。这将决定何时可以停止作业。

于 2013-03-14T22:28:02.543 回答