2013-03-14 41 views
1

我使用下面的代碼創建請稍候彈出的黑莓application.I想刪除彈出屏幕上回壓裝置的但我不能夠在這樣做,因爲 顯示的時候請等待彈出整個屏幕,直到線程運行完成。刪除,請等待彈出屏幕上的返回鍵

這裏是我的代碼:

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

什麼是代碼中的AnimatedGIFField。 – Mayank 2014-12-18 05:30:33

回答

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本身之間協同完成。

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()你傳遞給showScreenAndWait(),你將需要把一些檢查,看看是否該線程已經中斷:

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

你如何將這些檢查取決於任務。如果您使用run()方法下載10個文件,則可能需要在10次下載之間進行isInterrupted()檢查。如果run()包含一個while()循環,則每個循環檢查一次。這將確定工作可以停止。