Implementing the MIDlet main class

To implement the MIDlet main class:

  1. Create the TouristMIDlet.java class file.

  2. Assign the class to the com.nokia.example.location.tourist package.

    package com.nokia.example.location.touristroute;
  3. Import the required classes.

    import javax.microedition.lcdui.Display;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;
    
    import com.nokia.example.location.touristroute.model.ConfigurationProvider;
    import com.nokia.example.location.touristroute.model.ProviderStatusListener;
    import com.nokia.example.location.touristroute.model.TouristData;
    import com.nokia.example.location.touristroute.ui.MessageUI;
    import com.nokia.example.location.touristroute.ui.TouristUI;
  4. Set TouristMIDlet to extend MIDlet and implement ProviderStatusListener. Create the required variables and the class constructor.

    public class TouristMIDlet extends MIDlet implements ProviderStatusListener
    {
        /** A static reference to Display object. */
        private static Display display = null;
    
        /** A Reference to TouristData. */
        private TouristData data = null;
    
        /** Lock object */
        private Object mutex = new Object();
    
        public TouristMIDlet()
        {
            super();
        }
  5. Create the mandatory MIDlet lifecycle methods.

        protected void startApp() throws MIDletStateChangeException
        {
            display = Display.getDisplay(this);
    
            if (ConfigurationProvider.isLocationApiSupported())
            {
                ConfigurationProvider.getInstance().autoSearch(this);
            }
            else
            {
                MessageUI.showApiNotSupported();
            }
        }
    
        protected void pauseApp()
        {
        }
    
        protected void destroyApp(boolean unconditional)
                throws MIDletStateChangeException
        {
        }
  6. Create a method for getting a reference to the Display object.

        /**
         * Getter method for Display reference.
         *
         * @return reference to Display object.
         */
        public static Display getDisplay()
        {
            return display;
        }
  7. Create a method for indicating the selection of the location provider.

        /**
         * Event indicating location provider is selected. MIDlet use may therefore
         * begin.
         *
         * @see com.nokia.example.location.tourist.model.ProviderSelectedListener#providerSelectedEvent()
         */
        public void providerSelectedEvent()
        {
            // Attempt to acquire the mutex
            synchronized (mutex)
            {
                // Start scanning location updates. Also set the TouristData
                // reference data.
                MessageUI.showLocationProviderState();
    
                // Inform the user that MIDlet is looking for location data.
                data = new TouristData((ProviderStatusListener) this);
            }
        }
  8. Create a method for indicating that the first location update has occurred.

        /**
         * Event indication about the first location update. This method sets
         * Tourist UI visible. By using mutex object, we ensure TouristaData (data)
         * is created on providerSelectedEvent.
         *
         * @see com.nokia.example.location.tourist.model.ProviderStatusListener#firstLocationUpdateEvent()
         */
        public void firstLocationUpdateEvent()
        {
            // Attempt to acquire the mutex
            synchronized (mutex)
            {
                TouristUI ui = new TouristUI(data);
    
                data.setTouristUI(ui);
                display.setCurrent(ui);
            }
        }
    }

Now that you have implemented the MIDlet main class, implement the location services.