Creating the main MIDlet

MediaSamplerMIDlet class

  1. Create the MediaSamplerMIDlet class

  2. Import the required classes and assign this class to the com.nokia.example.mmapi.mediasampler package.

    package com.nokia.example.mmapi.mediasampler;
    
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    
    import com.nokia.example.mmapi.mediasampler.data.MediaFactory;
    import com.nokia.example.mmapi.mediasampler.viewer.MediaList;
    
    
  3. Set MediaSamplerMIDlet to extend MIDlet.

    /**
     * MMAPI example MIDlet class.
     */
    public class MediaSamplerMIDlet extends MIDlet
    {
    
  4. Write the below code to construct the Midlet.

        private final MediaList list;
    
        public MediaSamplerMIDlet()
        {
            MediaFactory.setMidlet(this);
            list = new MediaList(this);
        }
    
    

    Create the required startApp MIDlet method.

        /**
         * Overriden MIDlet method
         */
        public void startApp()
        {
            Displayable current = Display.getDisplay(this).getCurrent();
            if (current == null)
            {
                // first call
                Display.getDisplay(this).setCurrent(list);
            }
            else
            {
                Display.getDisplay(this).setCurrent(current);
            }
        }
    
    

    Create the other two required MIDlet methods

        /**
         * Overriden MIDlet method
         */
        public void pauseApp()
        {
    
        }
    
        /**
         * Overriden MIDlet method.
         */
        public void destroyApp(boolean unconditional)
        {
            list.releaseResources();
        }
    
    
  5. Create methods for exiting the MIDlet.

        /**
         * Exits the MIDlet. Called when exit command is selected from the
         * MediaList.
         */
        public void listExit()
        {
            exitRequested();
        }
    
        /**
         * Exits the MIDlet.
         */
        private void exitRequested()
        {
            destroyApp(false);
            notifyDestroyed();
        }
    
    
  6. Create a method for displaying error alerts.

        /**
         * Displays an error message in Alert.
         * 
         * @param message
         *            String as message to display.
         */
        public void alertError(String message)
        {
            Alert alert = new Alert("Error", message, null, AlertType.ERROR);
            Display display = Display.getDisplay(this);
            Displayable current = display.getCurrent();
            if (!(current instanceof Alert))
            {
                // This next call can't be done when current is an Alert
                display.setCurrent(alert, current);
            }
        }
    
    }