Slide show

MIDP - Mobile Information Device Profile - version 1.0


Portability


Networking with MIDP - Connector class


Connection hierachy


Connection subclasses


HttpConnection


HttpConnection example


    void getFromHttpConnection(String url) throws IOException {
         HttpConnection conn = null;
         InputStream is = null;
         try {
             conn = (HttpConnection )Connector.open(url);
 
             // Getting the InputStream will open the connection
             // and read the HTTP headers. They are stored until
             // requested.
             is = conn.openInputStream();
 
             // Get the ContentType
             String type = conn.getType();
 
             // Get the length and process the data
             int len = (int) conn.getLength();
             if (len > 0) {
                 byte[] data = new byte[len];
                 int actual = is.read(data);
                 ...
             } else {
                 int ch;
                 while ((ch = is.read()) != -1) {
                     ...
                 }
             }
         } finally {
             if (is != null)
                 is.close();
             if (conn != null)
                 conn.close();
         }
     }


Two-way communication using HttpConnection


MIDlet class


Application structure


Application shell



import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class Shell extends MIDlet {

    protected Display display;
    protected Displayable displayable;

    public Shell() {
	display = Display.getDisplay(this);
	displayable = new Form("a form");
    }

    public void startApp() {
	display.setCurrent(displayable);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
	display = null;
	displayable = null;
    }
}



Compiling and running

Assume you have unpacked the J2ME wireless toolkit into a directory such as WTK=/usr/local/personaljava/WTK2.0


Lists


List types


Command


CommandListener


Command listener example



import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class Command1 extends MIDlet implements CommandListener {

    protected Display display;
    protected List list;
    protected Command okCmd;
    protected Command helpCmd;

    public Command1() {
	display = Display.getDisplay(this);
	list = new List("Sample list",
			List.IMPLICIT,
			new String[] {"One", "Two", "Three"},
			null);

	okCmd = new Command("Okay", Command.OK, 1);
	helpCmd = new Command("Help", Command.HELP, 1);
	list.addCommand(okCmd);
	list.addCommand(helpCmd);
	list.setCommandListener(this);
    }

    public void commandAction(Command c, Displayable d) {
	// d == list
	if (c == okCmd) {
	    int index = list.getSelectedIndex();
	    String item = list.getString(index);
	    Alert selectedScr = new Alert("Selected item", 
				      "Item selected was " + item, 
				      null,
				      AlertType.INFO);
	    selectedScr.setTimeout(5000); // milliseconds
	    display.setCurrent(selectedScr);

	} else {
	    Alert helpScr = new Alert("Help", 
				      "Scroll to any item and press the select key", 
				      null,
				      AlertType.INFO);
	    display.setCurrent(helpScr);
	}
	// System.out.println("Selected index is " + list.getSelectedIndex());
    } 

    public void startApp() {
	System.out.println("Starting");
	display.setCurrent(list);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
	display = null;
	list = null;
    }
}



TextBox


TextBox example



import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class Text1 extends MIDlet {

    protected Display display;
    protected Displayable displayable;

    public Text1() {
	display = Display.getDisplay(this);
	displayable = new TextBox("Phone number", "+61",
				  12, TextField.PHONE);
    }

    public void startApp() {
	display.setCurrent(displayable);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
	display = null;
	displayable = null;
    }
}



Form


Form example



import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class Form1 extends MIDlet implements ItemStateListener {

    protected Display display;
    protected Form form;

    public Form1() {
	display = Display.getDisplay(this);
	form = new Form("Info entry form");
	form.append(new TextField("Name", "", 10, TextField.ANY));
	form.append(new Gauge("Age (0-100)", true, 10, 2));
	form.append(new ChoiceGroup("Job", Choice.EXCLUSIVE,
				 new String[] {"nerd", "student"},
				 null));
	form.setItemStateListener(this);
    }

    public void itemStateChanged(Item item) {
	System.out.println("State changed on item " +
			   item.toString());
    }

    public void startApp() {
	display.setCurrent(form);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
	display = null;
	form = null;
    }
}



Canvas


Graphics


Canvas drawing example



import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class Canvas1 extends MIDlet {

    protected Display display;
    protected Displayable displayable;

    public Canvas1() {
	display = Display.getDisplay(this);
	displayable = new MyCanvas();
    }

    public void startApp() {
	display.setCurrent(displayable);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
	display = null;
	displayable = null;
    }
}



import javax.microedition.lcdui.*;

public class MyCanvas1 extends Canvas {

    public void paint(Graphics g) {
	g.setColor(255, 0, 0);
	g.drawRect(10, 10, 100, 100);
	g.setColor(0, 255, 0);
	g.fillRect(20, 20, 80, 80);
    }
}


Canvas Listeners


Keycodes


Game actions


Canvas key example



import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class Canvas2 extends MIDlet {

    protected Display display;
    protected Displayable displayable;

    public Canvas2() {
	display = Display.getDisplay(this);
	displayable = new MyCanvas2();
    }

    public void startApp() {
	display.setCurrent(displayable);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
	display = null;
	displayable = null;
    }
}



import javax.microedition.lcdui.*;

public class MyCanvas2 extends Canvas {

    public void paint(Graphics g) {

    }

    protected void keyPressed(int keycode) {
	System.out.println("Keycode: " + keycode);
	System.out.println("Key (Unicode): " + getKeyName(keycode));
	System.out.println("Game action code: " + getGameAction(keycode));
	String gameAction = null;
	switch (getGameAction(keycode)) {
	case Canvas.DOWN: gameAction = "DOWN"; break;
	case Canvas.UP: gameAction = "UP"; break;
	case Canvas.LEFT: gameAction = "LEFT"; break;
	case Canvas.RIGHT: gameAction = "RIGHT"; break;
	case Canvas.FIRE: gameAction = "FIRE"; break;
	    // should be more valid cases here
	default: gameAction = "duh"; break;
	}
	System.out.println("Action: " + gameAction);
    }
}


Persistent store


Binary data


Binary data example


import java.io.*;

public class Person {

    public String firstName; // should be private
    public String lastName;  // ditto
    public int age;           // ditto

    public Person(String first, String last, int ag) {
	firstName = first;
	lastName = last;
	age = ag;
    }

    public Person(byte[] data) throws IOException {
	super();
	ByteArrayInputStream bis = new ByteArrayInputStream(data);
	DataInputStream dis = new DataInputStream(bis);
	firstName = DataInputStream.readUTF(dis);
	lastName = DataInputStream.readUTF(dis);
	age = dis.readInt();
    }

    public byte[] toBytes() {
	try {
	    ByteArrayOutputStream bos = new ByteArrayOutputStream();
	    DataOutputStream dos = new DataOutputStream(bos);
	    dos.writeUTF(firstName);
	    dos.writeUTF(lastName);
	    dos.writeInt(age);
	    dos.close();
	    return bos.toByteArray();
	}  catch(IOException e) {
	    // will only happen if e.g. out of memory - return empty
	    return new byte[] {};
	}
    }
}

Creating/opening RecordStore


Adding records


Record enumerator


Record store example



import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;

public class PersonBook extends MIDlet {

    protected Display display;
    protected List list;
    Person[] people;

    public PersonBook() {
	display = Display.getDisplay(this);
	list = new List("People list", List.IMPLICIT);
	// setup dummy data
	Person[] people = new Person[2];
	people[0] = new Person("Bill", "Smith", 30);
	people[1] = new Person("Fred", "Bloggs", 21);
	try {
	    saveToStore("personStore", people);
	} catch(RecordStoreException e) {
	    // ignore
	}
    }

    protected void saveToStore(String storeName, Person[] people)
	throws RecordStoreException {
	try {
	    // should backup first - but no copy/rename methods exist
	    RecordStore.deleteRecordStore(storeName);
	} catch(RecordStoreNotFoundException e) {
	    // ignore
	}

	RecordStore rs = RecordStore.openRecordStore(storeName, true);
	for (int n = 0; n < people.length; n++) {
	    byte[] data = people[n].toBytes();
	    int len = data.length;
	    rs.addRecord(data, 0, len);
	}
    }

    protected Person[] loadFromStore(String storeName)
	throws RecordStoreException {
	RecordStore rs = null;
	try {
	    rs = RecordStore.openRecordStore(storeName, false);
	} catch(RecordStoreException e) {
	    return null;
	}

	int numPeople = rs.getNumRecords();
	Person[] people = new Person[numPeople];
	int count = 0;
	RecordEnumeration enumerator = rs.enumerateRecords(null, null, false);
	while (enumerator.hasNextElement()) {
	    byte[] data = enumerator.nextRecord();
	    try {
		people[count] = new Person(data);
		count++;
	    } catch(IOException e) {
		people[count] = null;
	    }
	}
	return people;
    }

    public void startApp() {
	try {
	    people = loadFromStore("personStore");
	} catch(RecordStoreException e) {
	    // leave people as null
	}
	for (int n = 0; n < people.length; n++) {
	    if (people[n] != null)
		list.append(people[n].lastName, null);
	}
	display.setCurrent(list);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
	display = null;
    }
}



What's missing?


References



Jan Newmarch <jan@newmarch.name>
Last modified: Fri Dec 3 20:11:08 EST 2004
Copyright © Jan Newmarch, Monash University, 2007
Creative Commons License This work is licensed under a Creative Commons License
The moral right of Jan Newmarch to be identified as the author of this page has been asserted.