Java in the mobile

Posted: August 18, 2008 in Computer and Internet

A short note on how to get started on developing java for a mobile phone.

I am not fond of eclipse but for java development it gets the job done fairly well (the debugger integration really needs to be fixed, which is one of the main reason you (or at least me) actually chose an IDE over another)

There is one part of the Java standard that is specifically targeted for mobile devices, it is called J2ME, and we are going to leverage that here. You will need to following

  1. Install the JRE
  2. unzip eclipse.
  3. Install the WTK (remember where you put it, you will need it soon). Start eclipse and go to Help->Software updates. Add the eclipse me update site, install eclipseMe.
  4. Go to Window->preferences->J2ME, set the WTK Root.
  5. Go to device management, import, once again point to the WTK root, press refresh, import the devices.

A note, remember that eclipse is silly enough to store the devices for the WTK in the workspace, so if you create a new workspace you need to redo this operation (5). Never really understood the workspace idea in eclipse, I guess it is useful if you work on a million different things at once and want them separated, but it makes a mess keeping in synch.

You are now set to create your first Me application, there is a new project type called J2ME Midlet Suite, create one and start coding!

You will need to derive your main class from MIDlet, there are many samples out there in the web. Here is a very simple one

   1: import java.io.IOException;
   2: import java.io.InputStream;
   3:  
   4: import javax.microedition.io.Connector;
   5: import javax.microedition.io.HttpConnection;
   6: import javax.microedition.lcdui.Command;
   7: import javax.microedition.lcdui.CommandListener;
   8: import javax.microedition.lcdui.Display;
   9: import javax.microedition.lcdui.Displayable;
  10: import javax.microedition.lcdui.Form;
  11: import javax.microedition.lcdui.StringItem;
  12: import javax.microedition.lcdui.TextField;
  13: import javax.microedition.midlet.MIDlet;
  14: import javax.microedition.midlet.MIDletStateChangeException;
  15:  
  16: public class TestApp extends MIDlet implements CommandListener {
  17:  
  18:     private Command exitCommand = new Command("Exit", Command.EXIT, 1);
  19:     private Command connectCommand = new Command("Request", Command.OK, 1);
  20:     private boolean firstTime;
  21:     private Form mainForm;
  22:     private Display display;
  23:     private StringItem messageItem;
  24:  
  25:     public TestApp() {
  26:         firstTime = true;
  27:         mainForm = new Form("TestApp transaction request");
  28:         messageItem = new StringItem(null, "");
  29:     }
  30:  
  31:     protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  32:     }
  33:  
  34:     protected void pauseApp() {
  35:     }
  36:  
  37:     protected void startApp() throws MIDletStateChangeException {
  38:         if (firstTime) {
  39:             mainForm.append("This demo of TestApp, UserID and password");
  40:  
  41:             mainForm.append(new TextField("UserID", "", 15, TextField.NUMERIC));
  42:             mainForm.append(new TextField("Password", "", 4, TextField.PASSWORD | TextField.NUMERIC));
  43:             mainForm.append(messageItem);
  44:  
  45:             mainForm.addCommand(connectCommand);
  46:             mainForm.addCommand(exitCommand);
  47:             mainForm.setCommandListener(this);
  48:             firstTime = false;
  49:         }
  50:  
  51:         display = Display.getDisplay(this);
  52:         display.setCurrent(mainForm);
  53:     }
  54:  
  55:     public void commandAction(Command cmd, Displayable disp) {
  56:         if (cmd == exitCommand) {
  57:             try {
  58:                 destroyApp(false);
  59:             } catch (MIDletStateChangeException e) {
  60:                 e.printStackTrace();
  61:             }
  62:             notifyDestroyed();
  63:         } else if (cmd == connectCommand) {
  64:             Form waitForm = new Form("Waiting...");
  65:             display.setCurrent(waitForm);
  66:             Thread t = new Thread() {
  67:                 public void run() {
  68:                     connect();
  69:                 }
  70:             };
  71:             t.start();
  72:         }
  73:     }
  74:  
  75:     private void connect() {
  76:         HttpConnection hc = null;
  77:         InputStream in = null;
  78:         String url = "http://www.funky.uril/login.asp"; //getAppProperty("TestAppMIDlet.URL");
  79:  
  80:         try {
  81:             hc = (HttpConnection) Connector.open(url);
  82:             in = hc.openInputStream();
  83:  
  84:             int contentLength = (int) hc.getLength();
  85:             byte[] raw = new byte[contentLength];
  86:             int length = in.read(raw);
  87:  
  88:             in.close();
  89:             hc.close();
  90:  
  91:             // Show the response to the user.
  92:             String s = new String(raw, 0, length);
  93:             messageItem.setText(s);
  94:         } catch (Exception ioe) {
  95:             messageItem.setText(ioe.toString());
  96:         }
  97:         display.setCurrent(mainForm);
  98:     }
  99:  
 100: }

Collection of sun software

http://developers.sun.com/mobility/allsoftware/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s