检查JSR支持
检查JSR的支持简单的方式有两种:
1. 是通过System.getProperty("property_name")的方式进行判断,一般如果存在相关的APIs支持,它会返回一个非null字符串。
检测代码
public String getInfo(String info) {
if (info == null) {
return "<unknown>";
} else {
return info;
}
}
2. 通过Class.forName(clase_name)的方式。
try {
Class.forName(aClassName);
return true;
} catch (Exception e) {
return false;
}
}
上面的检测代码相对比较简单,而且也容易理解,关键是那些JSR 支持的属性名称,或者APIs的写法。
下面是部分属性名称,仅供参考。
System property | Description | Value |
microedition.platform | Defined in CLDC 1.0 and CLDC 1.1. | |
microedition.encoding | Always returns ISO-8859-1. | |
microedition.configuration | Defined in CLDC 1.0 and CLDC 1.1. | |
microedition.profiles | 依赖于底层实现 | |
microedition.locale* | JSR 37 | 依赖于底层实现 |
microedition.commports | 依赖于底层实现 | |
microedition.hostname | localhost | |
microedition.profiles | MIDP2.0 | |
file.separator | 文件分割符 | 依赖于底层实现(/,\) |
microedition.pim.version | JSR 75 | 1.0 |
microedition.smartcardslots | JSR 177 | 依赖于底层实现 |
microedition.location.version | JSR 179 | 1.0 |
microedition.sip.version | JSR 180 | 1.0 |
microedition.m3g.version | JSR 184 | 1.0 |
microedition.jtwi.version | JSR 185 | 1.0 |
wireless.messaging.sms.smsc | JSR 205 | 依赖于底层实现 |
wireless.messaging.mms.mmsc | JSR 205 | 依赖于底层实现 |
CHAPI-Version | JSR 211 | JSR 211 |
Nokia的一些系统参数 | ||
com.nokia.network.access | 网络参数 | pd - GSM pd.EDGE - EDGE pd.3G - 3G pd.HSDPA - 3G csd - GSM CSD/HSCSD bt_pan - Bluetooth PAN network wlan - WIFI na - 无任何网络 |
com.nokia.mid.dateformat | 日期格式 | Yy/mm/dd |
com.nokia.mid.timeformat | 时间格式 | hh:mm |
com.nokia.memoryramfree | 动态内存分配 Note: S60 第3版不支持 | |
com.nokia.mid.batterylevel | 电池状态 | |
com.nokia.mid.countrycode | 城市代码 | |
com.nokia.mid.networkstatus | 网络工作状态 | |
com.nokia.mid.networkavailability | 网络是否激活状态 | |
com.nokia.mid.networkid | 网络ID | 返回2个值 Network ID 网络简称 |
com.nokia.mid.networksignal | ||
com.nokia.mid.cellid | Cellid | 基站信息ID |
com.nokia.mid.imei | Imei号 | 手机唯一标识号 |
com.nokia.mid.imsi |
应用程序属性
应用程序属性值是在应用程序描述符文件或者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()来得到它,代码片断如下:
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。
简单的Demo
下面是简单的测试环境的代码,有经验的朋友可以很容易就就跑起来。
代码片段
* getSysInfo
*/
private void getSysInfo() {
addInfo("Microedition Configuration: ",
getInfo(System.getProperty("microedition.configuration")));
addInfo("Microedition Profiles: ",
getInfo(System.getProperty("microedition.profiles")));
addInfo("microedition.jtwi.version:",
getInfo(System.getProperty("microedition.jtwi.version")));
addInfo("microedition.platform:",
getInfo(System.getProperty("microedition.platform")));
addInfo("microedition.locale:",
getInfo(System.getProperty("microedition.locale")));
addInfo("default encoding:",
getInfo(System.getProperty("microedition.encoding")));
addInfo("microedition.commports",
getInfo(System.getProperty("microedition.commports")));
addInfo("microedition.hostname",
getInfo(System.getProperty("microedition.hostname")));
// microedition.smartcardslots
addInfo(" microedition.smartcardslots",
getInfo(System.getProperty(" microedition.smartcardslots")));
addInfo("com.nokia.network.access",
getInfo(System.getProperty("com.nokia.network.access")));
addInfo("com.nokia.mid.dateformat",
getInfo(System.getProperty("com.nokia.mid.dateformat")));
addInfo("com.nokia.mid.timeformat",
getInfo(System.getProperty("com.nokia.mid.timeformat")));
addInfo("com.nokia.memoryramfree",
getInfo(System.getProperty("com.nokia.memoryramfree")));
addInfo("com.nokia.mid.batterylevel",
getInfo(System.getProperty("com.nokia.mid.batterylevel")));
addInfo("com.nokia.mid.countrycode",
getInfo(System.getProperty("com.nokia.mid.countrycode")));
addInfo("com.nokia.mid.networkstatus",
getInfo(System.getProperty("com.nokia.mid.networkstatus")));
addInfo("com.nokia.mid.networksignal",
getInfo(System.getProperty("com.nokia.mid.networksignal")));
addInfo("com.nokia.mid.networkid",
getInfo(System.getProperty("com.nokia.mid.networkid")));
addInfo("com.nokia.mid.networkavailability",
getInfo(System.getProperty("com.nokia.mid.networkavailability")));
addInfo("com.nokia.mid.cellid",
getInfo(System.getProperty("com.nokia.mid.cellid")));
addInfo("com.nokia.mid.imei",
getInfo(System.getProperty("com.nokia.mid.imei")));
addInfo("com.nokia.mid.imsi",
getInfo(System.getProperty("com.nokia.mid.imsi")));
String[] timeZoneIDs = java.util.TimeZone.getAvailableIDs();
StringBuffer timeZonesBuffer = new StringBuffer();
for (int i = 0; i < timeZoneIDs.length; i++) {
timeZonesBuffer.append(timeZoneIDs[i]).append('\n');
}
addInfo("Total memory:",
Long.toString(Runtime.getRuntime().totalMemory()) + " bytes");
addInfo("Free memory:",
Long.toString(Runtime.getRuntime().freeMemory()) + " bytes");
addInfo("Available TimeZones:", timeZonesBuffer.toString());
addInfo("Default TimeZone:", java.util.TimeZone.getDefault().getID());
addInfo("com.siemens.mp.lcdui.Image", hasClassExit("com.siemens.mp.lcdui.Image") + "");
addInfo("com.motorola.phonebook.PhoneBookRecord", hasClassExit("com.motorola.phonebook.PhoneBookRecord") + "");
addInfo("com.motorola.Dialer", hasClassExit("com.motorola.Dialer") + "");
addInfo("com.jblend.util.Case", hasClassExit("com.jblend.util.Case") + "");
addInfo("com.samsung.util.AudioClip", hasClassExit("com.samsung.util.AudioClip") + "");
addInfo("com.mot.iden.multimedia.Lighting", hasClassExit("com.mot.iden.multimedia.Lighting") + "");
}
private boolean hasClassExit(String aClassName) {
try {
Class.forName(aClassName);
return true;
} catch (Exception e) {
return false;
}
}
public String getInfo(String info) {
if (info == null) {
return "<unknown>";
} else {
return info;
}
}
public void addInfo(String name, String value) {
iForm.append(new StringItem(name, value));
}
代码片段2
try {
Class.forName("javax.microedition.media.control.VideoControl");
addInfo("MMAPI: ", "yes" );
addInfo("MMAPI-Version: ", getInfo(System.getProperty("microedition.media.version")) );
} catch (ClassNotFoundException e) {
addInfo("MMAPI: ", "no" );
}
try {
Class.forName("javax.wireless.messaging.Message");
addInfo("WMAPI 1.1: ", "yes" );
try {
Class.forName("javax.wireless.messaging.MultipartMessage");
addInfo("WMAPI 2.0: ", "yes" );
} catch (ClassNotFoundException e) {
addInfo("WMAPI 2.0: ", "no" );
}
} catch (ClassNotFoundException e) {
addInfo("WMAPI 1.1: ", "no" );
}
try {
Class.forName("javax.bluetooth.DiscoveryAgent");
addInfo("Bluetooth-API: ", "yes" );
try {
Class.forName("javax.obex.ClientSession");
addInfo("Bluetooth-Obex-API: ", "yes" );
} catch (ClassNotFoundException e) {
addInfo("Bluetooth-Obex-API: ", "no" );
}
} catch (ClassNotFoundException e) {
addInfo("Bluetooth-API: ", "no" );
}
try {
Class.forName("javax.microedition.m3g.Graphics3D");
addInfo("M3G-API: ", "yes" );
} catch (ClassNotFoundException e) {
addInfo("M3G-API: ", "no" );
}
try {
Class.forName("javax.microedition.pim.PIM");
addInfo("PIM-API: ", "yes" );
} catch (ClassNotFoundException e) {
addInfo("PIM-API: ", "no" );
}
try {
Class.forName("javax.microedition.io.file.FileSystemRegistry");
addInfo("FileConnection-API: ", "yes" );
} catch (ClassNotFoundException e) {
addInfo("FileConnection-API: ", "no" );
}
try {
Class.forName("javax.microedition.location.Location");
addInfo("Location-API: ", "yes" );
} catch (java.lang.Throwable e) {
addInfo("Location-API: ", "no" );
}
try {
Class.forName("javax.microedition.xml.rpc.Operation");
addInfo("WebServices-API: ", "yes" );
} catch (ClassNotFoundException e) {
addInfo("WebServices-API: ", "no" );
}
try {
Class.forName("javax.microedition.sip.SipConnection");
addInfo("SIP-API: ", "yes" );
} catch (ClassNotFoundException e) {
addInfo("SIP-API: ", "no" );
}
try {
Class.forName("com.nokia.mid.ui.FullCanvas");
addInfo("Nokia-UI-API: ", "yes" );
} catch (ClassNotFoundException e) {
addInfo("Nokia-UI-API: ", "no" );
}
try {
Class.forName("com.siemens.mp.MIDlet");
addInfo("Siemens-Extension-API: ", "yes" );
try {
Class.forName("com.siemens.mp.color_game.GameCanvas");
addInfo("Siemens-ColorGame-API: ", "yes" );
} catch (ClassNotFoundException e) {
addInfo("Siemens-ColorGame-API: ", "no" );
}
} catch (ClassNotFoundException e) {
addInfo("Siemens-Extension-API: ", "no" );
}
}