J2ME规范包括了许多可选包,如支持多媒体功能的MMAPI,支持消息接收和发送的WMA,支持3D游戏的M3G API。如果某一款手机支持某个可选API,MIDP应用程序就可以使用它。但是,让用户回答“本机是否支持MMAPI”是不友好的,发布几个不同版本不但增加了开发的工作量,也让用户难以选择。因此,应用程序应该自己检测手机是否支持某一API,从而在运行期决定是否可以使用此API。
MIDP 1.0和2.0应用程序都可以通过System.getProperty(String key)检测某一个属性的信息。如果该属性有效,将返回对应的字符串,否则,返回null,表示系统不支持此功能。
例如,System.getProperty("microedition.profiles")可能的返回值是"MIDP-1.0"或"MIDP-2.0"。
以下是常见的系统属性和可选API的属性,右侧列出了可能的返回值:
系统信息
microedition.platform 平台名称,如j2me
microedition.configuration CLDC或CDC版本,如CLDC-1.0
microedition.profiles MIDP版本,如MIDP-1.0
microedition.encoding 默认的系统编码,如GBK
microedition.locale 默认的区域设置,如zh-CN
MMAPI相关
microedition.media.version MMAPI的版本,如1.1
supports.mixing 是否支持混音,如true
supports.audio.capture 是否支持音频捕获,如true
supports.video.capture 是否支持视频捕获,如true
supports.recording 是否支持录音,如true
audio.encodings 音频编码格式,如encoding=pcm encoding=pcm&rate=8000&bits=8&channels=1
video.snapshot.encodings 拍摄图片的编码格式,如encoding=jpeg encoding=png
streamable.contents 支持的流媒体格式,如audio/x-wav
WMA相关
wireless.messaging.sms.smsc 返回SMS的服务中心,如+8613800010000
wireless.messaging.mms.mmsc 返回MMS的服务中心,如http://mmsc.monternet.com
其他
microedition.m3g.version 返回Mobile 3D的版本,如1.0
bluetooth.api.version 返回蓝牙API的版本,如1.0
microedition.io.file.FileConnection.version 返回FileConnection的版本,如1.0
microedition.pim.version 返回PIM的版本,如1.0
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
/**
Creates the "Hello world" program in J2ME MIDP.
Note that the class must be public so that the device
application management software can instantiate it.
*/
public class HelloWorld extends MIDlet
{
...
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();
}
/**
Prints the values of the standard system properties
using the System.getProperty() call.
*/
protected void printSystemProperties()
{
String conf;
String profiles;
String platform;
String encoding;
String locale;
conf = System.getProperty("microedition.configuration");
System.out.println(conf);
profiles = System.getProperty("microedition.profiles");
System.out.println(profiles);
platform = System.getProperty("microedition.platform");
System.out.println(platform);
encoding = System.getProperty("microedition.encoding");
System.out.println(encoding);
locale = System.getProperty("microedition.locale");
System.out.println(locale);
System.out.println();
}
/**
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"));
}
}
例如,如果用户的手机内置了数码相机,并且支持MMAPI,我们就可以在MIDP程序中拍摄照片。因此,在应用程序启动时就应该判断是否启用拍照功能以及用户手机支持的图片编码格式:
boolean supports_take_photo = false;
boolean supports_jpeg_encoding = false;
boolean supports_png_encoding = false;
boolean supports_gif_encoding = false;
if(System.getProperty("microedition.media.version")!=null) {
if("true".equals(System.getProperty("supports.video.capture")))
supports_take_photo = true;
String all_encoding = System.getProperty("video.snapshot.encodings");
if(all_encoding!=null) {
if(all_encoding.indexOf("jpeg")!=(-1))
supports_jpeg_encoding = true;
if(all_encoding.indexOf("png")!=(-1))
supports_png_encoding = true;
if(all_encoding.indexOf("gif")!=(-1))
supports_gif_encoding = true;
}
}
}
The current set of system properties for use by the getProperty(String) method is returned as a Properties object. If there is no current set of system properties, a set of system properties is first created and initialized. This set of system properties always includes values for the following keys:
Key Description of Associated Value
java.version | Java Runtime Environment version |
java.vendor | Java Runtime Environment vendor |
java.vendor.url | Java vendor URL |
java.home | Java installation directory |
java.vm.specification.version | Java Virtual Machine specification version |
java.vm.specification.vendor | Java Virtual Machine specification vendor |
java.vm.specification.name | Java Virtual Machine specification name |
java.vm.version | Java Virtual Machine implementation version |
java.vm.vendor | Java Virtual Machine implementation vendor |
java.vm.name | Java Virtual Machine implementation name |
java.specification.version | Java Runtime Environment specification version |
java.specification.vendor | Java Runtime Environment specification vendor |
java.specification.name | Java Runtime Environment specification name |
java.class.version | Java class format version number |
java.class.path | Java class path |
java.library.path | List of paths to search when loading libraries |
java.io.tmpdir | Default temp file path |
java.compiler | Name of JIT compiler to use |
java.ext.dirs | Path of extension directory or directories |
os.name | Operating system name |
os.arch | Operating system architecture |
os.version | Operating system version |
file.separator | File separator ("/" on UNIX) |
path.separator | Path separator (":" on UNIX) |
line.separator | Line separator ("/n" on UNIX) |
user.name | User's account name |
user.home | User's home directory |
user.dir | User's current working directory |