Pregunta

Mi aplicación contiene una gran cantidad de imágenes. por lo que se necesita algún tiempo para cargar la aplicación. Quiero mostrar una pantalla de carga whhile se carga la aplicación. ¿Cómo es posible?

¿Fue útil?

Solución

Esta es una aplicación de ejemplo que los esqueletos de lo que busca hacer. Básicamente, la pantalla inicial se presiona una pantalla de carga. Durante la secuencia de arranque inicial que necesita para hacer girar un nuevo hilo, hacer tus cosas de carga y luego usar invokeLater a 1) asegurarse de que su en el despachador de eventos y 2) para empujar una nueva pantalla - o en el caso de este ejemplo, una diálogo -. a la pantalla para que el progreso del usuario lejos de la pantalla de carga

Aquí está el código:

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();
        }
    }
}

Otros consejos

                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();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top