MIDlet可以访问两种运行时的属性值:系统属性和应用程序属性。
1.系统属性
系统属性是在CLDC(Connected Limited Device Configuration)中定义的,属性值被写入底层的系统,我们可以读取它们,但是不能修改这些属性值。如果想读取一个系统属性值,那么可以使用System类的静态方法System.getProperty()来读取。有时候我们可能想读取特定手机的系统信息,比如IMEI号。解决办法就是去参考这款手机的开发指南和规范,比如,从SonyEricsson系列手机读取IMEI号,调用方法:System.getProperty ("com. onyericsson.imei"),这时将返回手机的IMEI号码,如IMEI 123456-00-123456-1-00。需要说明的一点是,在SonyEricsson P910上查询IMEI号码时,传递的字符串参数应该是com.sonyericsson.IMEI(IMEI须大写)。如果在不支持此项功能的手机上查询IMEI号时,将什么都不返回,例如,在T610上调用。

2.应用程序属性
应用程序属性值是在应用程序描述符文件或者MANIFEST文件中定义的,当我们部署应用程序的时候可以定义应用程序属性。比如下面是一个典型的JAD文件内容。

MIDlet-1: HttpWrapperMidlet,httpwrapper.HttpWrapperMIDlet

MIDlet-Jar-Size: 16315

MIDlet-Jar-URL: HttpWrapper.jar

MIDlet-Name: HttpWrapper

MIDlet-Vendor: Vendor

MIDlet-Version: 1.0

MicroEdition-Configuration: CLDC-1.0

MicroEdition-Profile: MIDP-1.0

Which-Locale: en

其中Which-Locale就是应用程序属性值,我们可以通过MIDlet的成员方法getAppProperty()来得到它,代码片断如下:

import javax.microedition.midlet.*;


public class MyMIDlet extends MIDlet {
    private String suiteName;
    private String which_locale;
    public MyMIDlet(){
        suiteName = getAppProperty( "MIDlet-Name" );
        which_locale = getAppProperty("Which-Locale");
    }
    //这里省略了其他代码
}

属性值对大小写是敏感的,如果属性值在底层系统、JAD文件和Manifest文件中都没有定义的话,那么将返回Null。

熟悉MIDP应用程序的系统属性对我们开发灵活、强大的MIDlet至关重要。可以根据系统属性返回值来动态地生成应用程序的菜单。例如,通过判断底层系统是否支持MMAPI来决定应用程序中是否要添加摄像功能。下面的例子演示了如何在MIDlet中读取系统属性。

package com.j2medev.ch3.property;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class DetectMIDlet extends MIDlet {
    private static DetectMIDlet instance = null;
    private Display display = null;
    private Displayable form = null;
    public DetectMIDlet() {
        instance = this;
    }
    protected void startApp() {
        if(display==null) {
            //首次启动应用程序
            display = Display.getDisplay(this);
            form = new TestForm();
            display.setCurrent(form);
        } else{
            display.setCurrent(form);
        }
    }
    protected void pauseApp() {}   
    protected void destroyApp(boolean unconditional) {}
    public static void quitApp() {
        instance.destroyApp(true);
        instance.notifyDestroyed();
        instance = null;
    }
}
class TestForm extends Form implements CommandListener
 {
    private Command exit = new Command("退出", Command.EXIT, 0); 
    public TestForm() {
        super("J2ME平台测试");
        String s;
        //CLDC版本属性
        s = System.getProperty("microedition.configuration");
        append(getValue("CLDC版本", s));
        //MIPD版本属性
        s = System.getProperty("microedition.profiles");
        append(getValue("MIDP版本",s));       
        s = System.getProperty("microedition.platform");
        append(getValue("软件平台", s));       
        s = System.getProperty("microedition.encoding");
        append(getValue("系统编码", s));       
        s = System.getProperty("microedition.locale");
        append(getValue("区域设置", s));       
        s = System.getProperty("microedition.jtwi.version");
        append(getValue("JTWI", s));       
        //判断是否支持MMAPI
        s = System.getProperty("microedition.media.version");
        append(getValue("MMAPI", s));
        //判断是否支持WMA
        s = System.getProperty("wireless.messaging.sms.smsc");
        if(s!=null) {
            append(getValue("WMA", "支持"));
            append(getValue("SMS", s));
            s = System.getProperty("wireless.messaging.mms.mmsc");
            append(getValue("MMS", s));
        } else
            append(getValue("WMA", null));
        //判断是否支持蓝牙
        s = System.getProperty("bluetooth.api.version");
        append(getValue("蓝牙", s));
        //判断是否支持个人信息管理
        s = System.getProperty("microedition.pim.version");
        append(getValue("PIM", s));
        //判断是否支持文件系统
        s=System.getProperty("microedition.io.file.FileConnection. version");
        append(getValue("FileConnection", s));
        //判断是否支持SIP
        s = System.getProperty("microedition.sip.version");
        append(getValue("SIP", s));
        //判断是否支持M3G JSR 184
        s = System.getProperty("microedition.m3g.version");
        append(getValue("M3G", s));
        addCommand(exit);
        setCommandListener(this);
    }
    private String getValue(String prompt, String s) 
    {
        return prompt + ":" + (s==null ? "不支持" : s) + "\n";
    }
    public void commandAction(Command c, Displayable d) {
        if(c==exit){
            DetectMIDlet.quitApp();
        }
    }
}