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.

    package com.nokia.example.fileselectexample;
  2. Import the required classes and packages.

    import javax.microedition.lcdui.Alert;
    import javax.microedition.lcdui.Display;
    import javax.microedition.midlet.MIDlet;
    
  3. Create the Main class to extend the MIDlet.

    public class Main extends MIDlet {
  4. Create the required variables and the constructor. Check availability of the File Select API using System.getProperty method.

    private MainView mainView;
    private final boolean isAPIAvailable;
    public Main() {
        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("File Select 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) {
    }
  6. Create the method to exit the application.

    public void quit() {
        destroyApp(true);
        notifyDestroyed();
    }