2009-10-01 5 views
2

Mon application contient beaucoup d'images. il faut donc du temps pour charger l'application. Je veux montrer un écran de chargement pendant que l'application est en cours de chargement. Comment est-ce possible ?Blackberry - Écran de chargement de l'application

+0

http://stackoverflow.com/search?q=splash+screen – Groo

Répondre

8

Voici un exemple d'application qui squelette ce que vous cherchez à faire. Fondamentalement, l'écran initial que vous appuyez est un écran de chargement. Au cours de la séquence de démarrage initiale, vous devez créer un nouveau thread, faire votre chargement, puis utiliser invokeLater pour 1) assurez-vous que le répartiteur d'événements et 2) pour pousser un nouvel écran - ou dans le cas de cet exemple un dialogue - à l'écran pour que l'utilisateur progresse loin de l'écran de chargement.

Voici le code:

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

Etes-vous sûr qu'il est correct d'exécuter la tâche de longue durée dans la méthode UIApplication.invokeLater . invokeLater exécute le code dans la méthode d'exécution fournie directement sur le thread du distributeur d'événements !!! – nixau

+0

@nixau C'est un point valide. J'ai retravaillé le code pour créer un thread séparé et j'utilise invokeLater uniquement pour la modification de l'interface graphique. Merci – Fostah

+0

Comment puis-je modifier le code si j'ai besoin d'ajouter un autre écran après le splash au lieu de l'alerte de dialogue? Merci – Bohemian

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

Nice et l'échantillon pour quelqu'un de nouveau à la plate-forme. Merci. –

Questions connexes