Implementing the main MIDlet

CapitalMIDlet class

This is the core class in the application. It controls the user interface and communicates with the Poster class that does the actual call of the Web Service. This class also implements PosterListener so that it can get the result of asynchronous remote calls.

To create this class:

  1. Create the class CapitalMIDlet.

  2. Assign the class to the example.capitals package and import the required classes.

    package example.capitals;
    
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;
    
    import example.capitals.ui.*;
  3. Set the class to extend MIDlet and to implement PosterListener. Implement the functionality of the class.

    /**
     * Main MIDlet class. This class takes care of handling the Display
     * and listens for the result of WebServices requests
     */
    public class CapitalMIDlet extends MIDlet implements PosterListener {
    
        // screen which is displayed at the moment
       private Screen currentScreen;
       // location of the web service
       private String endPoint;
       
       protected void startApp() throws MIDletStateChangeException {
          endPoint = getAppProperty("endPoint");
          // create a new WelcomeScreen and set it active
          changeScreen(new WelcomeScreen(this));      
       }
       
       protected void destroyApp(boolean unconditional)
          throws MIDletStateChangeException {
       }
    
       protected void pauseApp() {
       }
    
       /**
        * Change the currently active screen
        */   
       void changeScreen(Screen screen) {      
          currentScreen = screen;
          screen.makeActive();
       }
       
       /**
        * Called to force termination of MIDlet
         */
       public void quit()
       {       
          notifyDestroyed();
       }
        
       /**
        * This method calls the web service
        */
       public void requestCapital(String country) {
          changeScreen(new WaitingScreen(this));
          Poster poster = new Poster(this, endPoint);
          poster.requestCapital(country);
       }
        
       public void showMainScreen() {
          changeScreen(new MainScreen(this));
       }
    
       /**
        * Called when a valid result is retrieved from the
        * WebService
        */
       public void onCapitalRequestComplete(String nation, String capital){
          changeScreen(new ResultScreen(this, nation, capital)); 
       }
    
       /**
        * Called when an error happens during the WebService call
        */
       public void onCapitalRequestError(String code){
          changeScreen(new ErrorScreen(this, code));
       }
       
    }