Application Properties
In chapter 2, you learned about the presence of certain MIDlet attributes that are defined in the JAD file of each MIDlet suite. Recall that all MIDlets have required attributes. Table 2.4 in chapter 2 listed the required MIDlet attributes that reside in the application descriptor file. A MIDlet can access the values of these attributes at runtime through the application management software.
When the AMS installs a MIDlet suite on the device, it places the MIDlet JAD file in a specific location under its control. When a MIDlet is started, the AMS reads the JAD file and builds a data structure of application attributes. A MIDlet can read the value of an attribute using the MIDlet class method
String getAppProperty(String key)
The key parameter is the name of the attribute, for example MIDlet-Name. The string returned is the associated value found in the JAD file.
Listing 3.3 demonstrates how a MIDlet can retrieve attributes. It modifies Listing 3.2 by adding a call to the printAppProperties() method at the end of the startApp() method. The new startApp() method follows:
Listing 3.3 The modified method now also prints the application properties. The device AMS software manages application properties.
public void startApp() { // Create a Displayable widget. form = new Form("Hello, World"); // Add a string to the form. String msg = "My first MIDlet!"; form.append(msg); // This app simply displays the single form created // above. display = Display.getDisplay(this); display.setCurrent(form); printSystemProperties(); printAppProperties(); }
The method shown in Listing 3.3 prints the values of the standard MIDlet application properties to standard output. Listing 3.4 shows the printAppProperties() method.
Listing 3.4 MIDlet attributes, or properties, are different from system properties. You can define an unlimited number of optional MIDlet attributes in addition to the predefined, required ones.
/** Prints application properties using the MIDlet.getAppProperty() call. */ protected void printAppProperties() { System.out.println(getAppProperty("MIDlet-Name")); System.out.println(getAppProperty("MIDlet-Jar-Size")); System.out.println(getAppProperty("MIDlet-Jar-URL")); System.out.println(getAppProperty("MIDlet-Vendor")); }
This latest version of the HelloWorld program now prints the following lines to standard output, which you should see in your Wireless Toolkit main console window. The printAppProperties() method prints the last four lines of output.
CLDC-1.0 MIDP-1.0 j2me ISO-8859-1 en_US HelloWorld 6781 HelloWorld.jar Vartan Piroumian
The four attributes accessed in Listing 3.4 are standard applications properties available to all MIDlets. Recall from chapter 2, however, that some additional required MIDlet attributes are defined in Table 2.4. Also, the MIDP specification defines several optional application attributes; Table 2.5 in chapter 2 lists these optional attributes. Your applications have access to all of them through the mechanism demonstrated in Listings 3.3 and 3.4.
Additionally, MIDlets can define optional, application-specific attributes. You can define as many application-specific properties as you like. Your application would then access them using the MIDlet.getAppProperty() method demonstrated in Listings 3.3 and 3.4. This capability is a kind of configuration or customization mechanism for MIDlets. You'll see some examples of custom attribute definition and use in chapter 9.