Implementing the MIDlet Main class

The Main class is the MIDlet main class. When the MIDlet is launched, the Main class is loaded first.

To implement the Main class:

  1. Create the Main.java class file.

  2. Import the required packages and classes.

    package com.nokia.example.imagescaler;
    
    import javax.microedition.lcdui.Alert;
    import javax.microedition.lcdui.Display;
    import javax.microedition.midlet.MIDlet;
  3. Create the Main class to extend the MIDlet. The Main class owns the object of the MainView (UI class).

    public class Main extends MIDlet
    {
        private MainView mainView;
        private final boolean isAPIAvailable;
  4. Create the Main class constructor.

    public Main() {
        // Checks whether the FileSelect API is available or not
        isAPIAvailable = System.getProperty("microedition.io.file.FileConnection.version") != null;
    }
  5. Define the mandatory lifecycle methods for starting, pausing, and destroying the MIDlet.

    public void startApp() {
        if (isAPIAvailable) {
            mainView = new MainView("Image Scaler Example", this);
            Display.getDisplay(this).setCurrent(mainView);
        }
        else {
            String text = getAppProperty("MIDlet-Name") + "\n"
                          + getAppProperty("MIDlet-Vendor")
                          + "\nFile Connection API is not available!";
            Alert alert = new Alert(text);
            Display.getDisplay(this).setCurrent(alert);
        }
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
    
    public void quit() {
        destroyApp(true);
        notifyDestroyed();
    }
    }