Implementing the main MIDlet

TextHandler class

TextHandler is the starting point of the application

  1. Create the TextHandler class file.

  2. Import the required classes and assign this class to the com.nokia.midp.example.jsr211.texthandler package.

    package com.nokia.midp.example.jsr211.texthandler;
    
    import javax.microedition.content.ContentHandlerServer;
    import javax.microedition.content.Invocation;
    import javax.microedition.content.Registry;
    import javax.microedition.content.RequestListener;
    import javax.microedition.lcdui.Alert;
    import javax.microedition.lcdui.AlertType;
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Form;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;
    
  3. Create the main class and the objects to be used in this application:

    public class TextHandler extends MIDlet implements CommandListener, RequestListener {
    
    	/**Confirmation screen title*/
    	private static final String CONFIRM = "Confirmation";
    
    	/**Choice list title*/
    	private static final String FAVORITE_LINKS = "Favorite Links";
    
    	/** content type constant*/
    	private static final String CONTENT_TYPE = "text/plain";
    
    	/** Current class name*/
    	private static final String CLASS_NAME = "com.nokia.midp.example.jsr211.texthandler.TextHandler";
    
    	/** Display instance*/
    	Display display = null;
    
    	/** Proceed the invocation confirmation*/
    	private Command proceedCommand = new Command("Proceed", Command.OK, 1);
    
    	/** Go back to the choice list window*/
    	private Command backCommand = new Command("Back", Command.OK, 2);
    
    	/** Exit the application and go back to the invoker*/
    	private Command exitCommand = new Command("Exit", Command.BACK, 2);
    
    	/** Current invocation, null if no Invocation. */
    	private Invocation invocation;
    
    	/** ContentHandlerServer from which to get requests. */
    	private ContentHandlerServer handler;
    
    	/** Access to Registry functions and responses. */
    	private Registry registry;
    
    	/**instance to display choice list*/
    	ChoiceList choices;
    
    	/**Invocation URL*/
    	String url;
  4. Instantiate the Display as the starting point of the MIDlet and create a list for the user to choose.

    	/**
    	 * Constructor creates Display instance and create choice list.
    	 */
    	public TextHandler() {
    		display = Display.getDisplay(this);
    
    		//Display the choice list
    		choices = new ChoiceList(FAVORITE_LINKS, this);
    	}

    Create the required default MIDlet methods.

    	protected void startApp() throws MIDletStateChangeException {
    		display.setCurrent(choices);
    	}
    
    	protected void destroyApp(boolean arg0) {
    		notifyDestroyed();
    	}
    
    	protected void pauseApp() {
    	}
  5. Add a method to display text invocation for content type text/plain.

    	/**
    	 * @param invoc invocation with content type "text/plain"
    	 */
    	void showCurrentInvocation(Invocation invoc) {
    		Form textViewerFrom = new TextViewer(invoc, this);
    		display.setCurrent(textViewerFrom);
    	}
  6. Add an Alert screen that is shown to the user before going further.

    	/**
    	 * @param message Message to display
    	 **/
    	private void displayMessage(String message) {
    		Alert alert = new Alert("Message", message, null, AlertType.INFO);
    		display.setCurrent(alert);
    	}

    Add a confirmation dialog.

    	/**
    	 * @param msg Message to display on confirmation screen
    	 * */
    	private void confirmationScreen(String msg) {
    		Form confirmForm = new Form(CONFIRM);
    		confirmForm.addCommand(backCommand);
    		confirmForm.addCommand(proceedCommand);
    		confirmForm.append(msg);
    		confirmForm.setCommandListener(this);
    		display.setCurrent(confirmForm);
    	}
  7. Add Command handling for the options available at this point.

    	/**
    	 * Handle Invocation commands
    	 */
    	public void commandAction(Command command, Displayable disp) {
    
    		if (command == proceedCommand) {
    			doInvoke(url);
    		} else if (command == backCommand) {
    			display.setCurrent(choices);
    		} else if (command == exitCommand) {
    			destroyApp(true);
    		}
    	}

    Create a method using the CHAPI instance ContentHandlerServer for handling incoming invocation requests. In this example, the current invocation is always finished before proceeding to the next one.

    	public void invocationRequestNotify(ContentHandlerServer server) {
    
    		if (invocation != null) {
    			server.finish(invocation, Invocation.OK);
    		}
    		invocation = server.getRequest(false);
    		if (invocation != null) {
    			showCurrentInvocation(invocation);
    		}
    	}

    Create a method for finalizing the invocation process.

    	/**
    	 * @param invocStatus
    	 */
    	void doFinish(int invocStatus) {
    
    		if (invocStatus == Invocation.OK) {
    			if (invocation != null)
    				invocation = null;
    
    			destroyApp(true);
    		}
    	}

    Create a method for invoking the URL in question.

    	/**
    	 * @param url http URL
    	 */
    	void doInvoke(String url) {
    		try {
    			Invocation invoc = new Invocation(url);
    			if (registry.invoke(invoc)) {
    				destroyApp(true);
    			} else {
    			}
    		} catch (Exception ex) {
    			displayMessage("Could not link to " + url);
    		}
    	}
    }