Mobile Information Device Profile MIDP User Interface APIs

Chapter 8 - MIDP User Interface (UI) API

    Mobile Information Device Profile MIDP User Interface APIs

  • MIDP User Interface APIs

    The MIDP user interface API is divided into a high- and low-level API. The high-level API provides input elements such as text fields, choices, and gauges.

    User Interface Architecture

    MIDP 2.0 provides UI classes in two packages, javax.microedition.lcdui and javax.microedition.lcdui.game, where lcdui stands for liquid crystal display user interface (LCD UI).

    The UI classes of of MIDP 2.0's javax.microedition.lcdui package can be divided into two logical groups: the high- and low-level groups.

    The classes of the high-level group are perfect for development of MIDlets that target the maximum number of devices, because these classes do not provide exact control over their display.

    UI Architecture

    There are only two fixed levels: Screens and Items

    The Items can be placed in a Form, which is a specialized Screen.The high-level Screens and the low-level class Canvas have the common base class Displayable.

    All subclasses of Displayable fill the whole screen of the device.Subclasses of Displayable can be shown device using the setCurrent() method of the Display object.

    The display hardware of a MIDlet can be accessed by calling the static method getDisplay(), where the MIDlet itself is given as parameter.

        Display display = Display.getDisplay (this);
        ...
        display.setCurrent (mainForm);
    

    The Mobile Information Device Profile (MIDP) includes both a low-level user interface (UI) API and a high-level UI API. The low-level API gives you complete access to a device's screen and to raw key and pointer events.

    import javax.microedition.lcdui.*;
    
    Image icon = ...; // code omitted
    
    Alert msg = new Alert( "Error!" );
    msg.setImage( icon );
    msg.setString( "No connection was possible." );
    msg.setTimeout( 5000 ); // in milliseconds
    msg.setType( AlertType.ERROR );
    

    Specify all properties, except for the timeout value, in the constructor. For example:

    Alert msg = new Alert( "Error!",
                           "No connection was possible.",
                           icon,
                           AlertType.ERROR );
    msg.setTimeout( Alert.FOREVER ); 
    

    To return to the displayable element that was active just before the alert was displayed:

    Display display = ....; // assign on startup
    Alert   msg = ....; // create an alert
    display.setCurrent( alert, display.getCurrent() );

    Example:


    
    

    public MyApp extends MIDlet

    {

    private Display display;

    public MyApp()

    {

    display = Display.getDisplay( this );

    }

    public Display getDisplay()

    {

    return display;

    } // rest of MIDlet goes here

    }

    Play any sound at any time by calling the playSound method:

        AlertType.ERROR.playSound( display );
    

    Create an instance of the Ticker class, passing the string displayed into the constructor:

    Example:

    import javax.microedition.ldcdui.*;
    
    Ticker ticker = new Ticker( "Hello, world!" );
    To display the ticker, you associate it with a top-level window created by the high-level UI, that is, any class that extends the Screen class. You associate the ticker by calling the setTicker method, as in: 
    
    Form f = new Form( "A Title" );
    f.setTicker( ticker );
    

    Example:stock tracking MIDlet that use of alerts and tickers.The stock values are generated randomly for purposes.Use the HttpConnection class to obtain stock values from a web site.

    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import java.util.*;
    
    public class StockWatcher extends MIDlet {
    
        Display      display;
        Ticker       ticker = new Ticker( "" );
        Command      exitCommand = new Command( 
                               "Exit", Command.EXIT, 1 );
        Timer        timer = new Timer();
        StockChecker checker = new StockChecker();
        TickerForm   form = new TickerForm();
        Alert        alert = new Alert( "Stock Alert!" );
    
        public StockWatcher() {
            display = Display.getDisplay( this );
            alert.setTimeout( Alert.FOREVER );
        }
    
        protected void destroyApp( boolean unconditional ) {
        }
    
        protected void startApp() {
            display.setCurrent( form );
            timer.schedule( checker, 0, 30000 );
        }
    
        protected void pauseApp() {
        }
    
        public void exit(){
            timer.cancel();
            destroyApp( true );
            notifyDestroyed();
        }
    
        // Display a simple form to hold the ticker
    
        class TickerForm extends Form implements
                                       CommandListener {
            public TickerForm(){
                super( "Stock Watch" );
                setTicker( ticker );
                addCommand( exitCommand );
                setCommandListener( this );
            }
    
            public void commandAction( Command c,
                                        Displayable d ){
                exit();
            }
        }
      
        // Check the stock values and put up an alert if
        // they're beyond certain limits....
    
        class StockChecker extends TimerTask {
            Random       generator = new Random();
            int          sybsValue = 20000;
            int          sunwValue = 30000;
            int          ibmValue = 40000;
            StringBuffer buf = new StringBuffer();
    
            public void run(){
                String values = getStockValues();
    
                ticker.setString( values );
    
                if( sybsValue < 18000 || sybsvalue > 22000 ||
                    sunwValue < 28000 || sunwvalue > 32000 ||
                    ibmValue < 38000 || ibmvalue > 42000 ){
                    alert.setString( values );
                }
    
                if( !alert.isShown() ){
                  display.setCurrent( alert, form );
                }
            }
    
            private String getStockValues(){
                sybsValue = randomStockValue( sybsValue );
                sunwValue = randomStockValue( sunwValue );
                ibmValue = randomStockValue( ibmValue );
    
                buf.setLength( 0 );
                appendValue( "SYBS", sybsValue );
                appendValue( "SUNW", sunwValue );
                appendValue( "IBM", ibmValue );
    
                return buf.toString();
            }
    
            // Generate a random stock value... in the
            // real world you'd use HTTP to obtain the
            // stock value from a broker's website.
    
            private int randomStockValue( int oldVal ){
                int incr1 = ( generator.nextInt() % 2 );
                int incr2 = ( generator.nextInt() % 16 );
    
                if( incr1 < 1 ){
                    oldval -= incr1 * 1000;
                } else {
                    oldval += ( incr1 - 2 ) * 1000;
                }
    
                if( incr2 < 8 ){
                    oldval -= incr2 * 250;
                } else {
                    oldval += incr2 * 250;
                }
    
                return oldval;
            }
    
            private void appendvalue( string stock, int val ){
                buf.append( stock );
                buf.append( ' ' );
                buf.append( integer.tostring( val / 1000 ) );
                buf.append( '.' );
                buf.append( integer.tostring( val % 1000 ) );
                buf.append( ' ' );
            }
        }
    }
    

© 2015 by Learncertification All Rights Reserved. The certification names are the trademarks of their respective owners. Terms & Privacy Policy