Archive

Posts Tagged ‘Java ME’

Multiple Commands In A Midlet

July 8th, 2009 No comments

I was recently asked what difference would it make if I chose between the command types Command.SCREEN, Command.ITEM and Command.OK in a screen in a midlet with multiple menu items appearing in way similar to the screenshot below.

Midlet Screen With Multiple Menu Items

Figure 1 : Midlet Screen With Multiple Menu Items

Some simple code (below) that produces the above effect.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
 
public class MutipleCommandMidlet extends MIDlet implements CommandListener
{
  private Command exitCmd, menuItem1, menuItem2, menuItem3, menuItem4, menuItem5, menuItem6,
    menuItem7, menuItem8;
  private Form form;
 
  public MutipleCommandMidlet()
  {
    exitCmd = new Command("Exit", Command.EXIT, 1);
    menuItem1 = new Command("Item 1", Command.SCREEN, 2);
    menuItem2 = new Command("Item 2", Command.SCREEN, 3);
    menuItem3 = new Command("Item 3", Command.SCREEN, 4);
    menuItem4 = new Command("Item 4", Command.SCREEN, 5);
    menuItem5 = new Command("Item 5", Command.SCREEN, 6);
    menuItem6 = new Command("Item 6", Command.SCREEN, 7);
    menuItem7 = new Command("Item 7", Command.SCREEN, 8);
    menuItem8 = new Command("Item 8", Command.SCREEN, 9);
    form = new Form("Some Form");
  }
 
  public void commandAction(Command c, Displayable d)
  {
    String label = c.getLabel();
    if (label.startsWith("Menu Item"))
      showAlert("SCREEN Command", label + " Pressed");
    else if (label.equals("Exit"))
      showAlert("EXIT Command", label + " Pressed");
  }
 
  protected void destroyApp(boolean arg0) throws MIDletStateChangeException  { /* not implemented */ }
 
  protected void pauseApp() {  /* not implemented */ }
 
  private void showAlert(String alertTitle, String alertText)
  {
    System.out.println(alertTitle + " : " + alertText);
    Alert alert = new Alert(alertTitle, alertText, null, AlertType.INFO);
    alert.setTimeout(2000);
    Display.getDisplay(this).setCurrent(alert, form);
  }
 
  protected void startApp() throws MIDletStateChangeException
  { // Note that it does not matter which order the menu items are added
    form.addCommand(menuItem1);
    form.addCommand(menuItem3);
    form.addCommand(menuItem2);
    form.addCommand(menuItem4);
    form.addCommand(menuItem5);
    form.addCommand(menuItem6);
    form.addCommand(menuItem7);
    form.addCommand(menuItem8);
    form.addCommand(exitCmd);
    form.setCommandListener(this);
    Display.getDisplay(this).setCurrent(form);
  }// end-of-startApp
}

Take note of lines 11 to 20 and also lines 47 to 55

11
12
13
14
15
16
17
18
19
    exitCmd = new Command("Exit", Command.EXIT, 1);
    menuItem1 = new Command("Item 1", Command.SCREEN, 2);
    menuItem2 = new Command("Item 2", Command.SCREEN, 3);
    menuItem3 = new Command("Item 3", Command.SCREEN, 4);
    menuItem4 = new Command("Item 4", Command.SCREEN, 5);
    menuItem5 = new Command("Item 5", Command.SCREEN, 6);
    menuItem6 = new Command("Item 6", Command.SCREEN, 7);
    menuItem7 = new Command("Item 7", Command.SCREEN, 8);
    menuItem8 = new Command("Item 8", Command.SCREEN, 9);
47
48
49
50
51
52
53
54
55
    form.addCommand(menuItem1);
    form.addCommand(menuItem3);
    form.addCommand(menuItem2);
    form.addCommand(menuItem4);
    form.addCommand(menuItem5);
    form.addCommand(menuItem6);
    form.addCommand(menuItem7);
    form.addCommand(menuItem8);
    form.addCommand(exitCmd);

Now if we were to change the ordering in which the Commands are being added to the Form, to say something like this:

47
48
49
50
51
52
53
54
55
    form.addCommand(menuItem5);
    form.addCommand(menuItem4);
    form.addCommand(menuItem1);
    form.addCommand(menuItem3);
    form.addCommand(menuItem2);
    form.addCommand(menuItem6);
    form.addCommand(menuItem7);
    form.addCommand(menuItem8);
    form.addCommand(exitCmd);

we would still get the screen looking like Figure 1 above. The reason (I think) this happens is because of the priorities (5 and 6 respectively) assigned to menuItem4 and menuItem5.

15
16
    menuItem4 = new Command("Item 4", Command.SCREEN, 5);
    menuItem5 = new Command("Item 5", Command.SCREEN, 6);

If we were to change lines 12 to 15, so that menuItem1, menuItem4 and menuItem5 now have the same priority (level 2),

12
13
14
15
16
    menuItem1 = new Command("Item 1", Command.SCREEN, 2);
    menuItem2 = new Command("Item 2", Command.SCREEN, 3);
    menuItem3 = new Command("Item 3", Command.SCREEN, 4);
    menuItem4 = new Command("Item 4", Command.SCREEN, 2);
    menuItem5 = new Command("Item 5", Command.SCREEN, 2);

then we get the menu items appearing similar to the screenshot below.

Figure 2 : Midlet Screen With Multiple Menu Items

Figure 2 : Midlet Screen With Multiple Menu Items (Equal Priority Levels)


Here, the menu items, menuItem1, menuItem4 and menuItem5 (which have equal priority levels) appeared in the order in which they were added to the Form.

Categories: Java Tags: ,

How To Detect If A Java ME Phone Supports A Particular JSR

July 6th, 2009 No comments

A few weeks ago, I had the need to detect if a Java enabled phone supported JSR 179 (Location API for J2ME). Since then, I came accross the need to detect support for other JSRs.

The required information can be determined by calling
System.getProperty(propertyName);
If the string returned by the getProperty returns a non NULL string, it implies the particular JSR is supported.

Here is a summary of a few JSRs along with their corresponding propertyNames :

JSR 75 (FC) microedition.io.file.FileConnection.version
JSR 75 (PIM API) microedition.pim.version
JSR 82 javax.bluetooth.LocalDevice
JSR 120 javax.wireless.messaging.MessageConnection
JSR 135 (MMAPI), Check If Audio Capture Supported supports.audio.capture
JSR 135 (MMAPI), Check If Video Capture Supported supports.video.capture
JSR 179 microedition.location.version
Categories: Java Tags:

How To Detect If A Java ME Phone Supports JSR 179

June 2nd, 2009 No comments

I recently had a need to detect whether a Java enabled phone supported JSR 179 (Location API for J2ME).
Here’s a very simple way, just call
String locationAPIVersionStr = System.getProperty("microedition.location.version");
If the locationAPIVersionStr returns a non NULL string, it implies JSR 179 is supported.

Categories: Java Tags: