2009-10-01 197 views
2

我的應用程序包含大量的圖像。所以加載應用程序需要一些時間。我想在應用程序正在加載時顯示加載屏幕。這怎麼可能 ?黑莓 - 應用程序加載屏幕

+0

http://stackoverflow.com/search?q=splash+screen – Groo 2009-10-01 11:17:46

回答

8

下面是一個示例應用程序,該應用程序骷髏您要做的事情。基本上,你推動的初始屏幕是一個加載屏幕。在最初的啓動過程中,你需要啓動一個新的線程,執行你的加載,然後使用invokeLater來1)確保你在事件調度程序中和2)推動一個新的屏幕 - 或者在這個例子中對話框 - 屏幕上讓用戶離開加載屏幕。

下面的代碼:

import net.rim.device.api.ui.Field; 
import net.rim.device.api.ui.UiApplication; 
import net.rim.device.api.ui.container.MainScreen; 
import net.rim.device.api.ui.component.Dialog; 
import net.rim.device.api.ui.component.LabelField; 

/** 
* Just a test app. 
*/ 
public class TestAppMain extends UiApplication 
{ 
    /** 
    * Default Constructor. 
    */ 
    private TestAppMain() {     
     pushScreen(new AppScreen(this));   
    } 

    /** 
    * App entry point. 
    * @param args Arguments. 
    */ 
    public static void main(String[] args) { 
     TestAppMain app = new TestAppMain();      
     app.enterEventDispatcher(); 
    } 

    /** 
    * Main application screen. 
    */ 
    private static class AppScreen extends MainScreen 
    { 
     UiApplication _app; 

     /** 
     * Default constructor. 
     */ 
     public AppScreen(UiApplication app) { 
      // Note: This screen just says "Loading...", but you could 
      // add a loading animation. 
      _app = app; 
      LabelField title = new LabelField("App Name", 
        LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH); 
      setTitle(title); 

      LabelField loading = new LabelField("Loading...", 
        LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH); 

      add(loading); 

      // Queue up the loading process. 
      startLoading(); 
     }    

     /** 
     * Create the loading thread. Make sure to invoke later as you will 
     * need to push a screen or show a dialog after the loading is complete, eventhough 
     * you are creating the thread before the app is in the event dispatcher. 
     */ 
     public void startLoading() { 
      Thread loadThread = new Thread() { 
       public void run() { 
        // Make sure to invokeLater to avoid problems with the event thread. 
        try{ 
         // Simulate loading time 
         Thread.sleep(5000); 
        } catch(java.lang.InterruptedException e){} 

        // TODO - Add loading logic here. 

        _app.invokeLater(new Runnable() {     
         public void run() {      
          // This represents the next step after loading. This just shows 
          // a dialog, but you could push the applications main menu screen. 
          Dialog.alert("Load Complete"); 
         } 
        }); 
       } 
      }; 
      loadThread.start(); 
     } 
    } 
} 
+1

你確定這是正確的UIApplication.invokeLater方法來執行長時間運行的任務。 invokeLater在事件分派器線程上執行提供的運行方法中的代碼! – nixau 2009-10-02 14:50:44

+0

@nixau這是一個有效的觀點。我重寫了代碼以啓動一個單獨的線程,並且只使用invokeLater進行GUI更改。謝謝 – Fostah 2009-10-03 15:16:38

+0

如果我需要在飛濺之後添加另一個屏幕而不是對話框警報,我該如何修改代碼?謝謝 – Bohemian 2009-11-05 13:50:50

1
   HorizontalFieldManager popHF = new HorizontalFieldManager(); 
       popHF.add(new CustomLabelField("Pls wait...")); 
       final PopupScreen waitScreen = new PopupScreen(popHF); 
       new Thread() 
       { 
        public void run() 
        { 

         synchronized (UiApplication.getEventLock()) 
         { 
          UiApplication.getUiApplication().pushScreen(waitScreen); 
         } 
         //Here Some Network Call 

         synchronized (UiApplication.getEventLock()) 
         { 
          UiApplication.getUiApplication().popScreen(waitScreen); 
         } 
        } 
       }.start(); 
+1

對於新來的平臺來說,這是一個不錯的例子。謝謝。 – 2013-09-06 12:31:12