兩類屬性來源
- 系統屬性:由 CLDC/MIDP/可選包規範定義,
System.getProperty(key) 讀取,只讀
- 應用屬性:定義於 JAD 或 MANIFEST.MF,
MIDlet.getAppProperty(key) 讀取,部署時可配置
完整系統屬性參考表 (按 JSR 分組)
| JSR | 屬性名 | 默認值/說明 |
| 30 (CLDC 1.0) | microedition.platform | null (設備平台名) |
| microedition.encoding | ISO8859_1 (默認字符編碼) |
| microedition.configuration | CLDC-1.0 |
| microedition.profiles | null |
| microedition.locale | null (當前語言環境) |
| 37 (MIDP 1.0) | microedition.profiles | MIDP-1.0 |
| 75 (PIM/File) | microedition.io.file.FileConnection.version | 1.0 |
| 75 | file.separator | 實現相關 (如 "/") |
| 75 | microedition.pim.version | 1.0 |
| 118 (MIDP 2.0) | microedition.locale | null |
| microedition.profiles | MIDP-2.0 |
| microedition.commports | 實現相關 (串口列表) |
| microedition.hostname | 實現相關 (設備主機名) |
| 120 (WMA 1.1) | wireless.messaging.sms.smsc | 實現相關 (短信中心號碼) |
| 139 (CLDC 1.1) | microedition.platform | 實現相關 |
| microedition.encoding | ISO8859-1 |
| microedition.configuration | CLDC-1.1 |
| microedition.profiles | 實現相關 |
| 177 (SATSA) | microedition.smartcardslots | 實現相關 (智能卡槽數) |
| 179 (Location) | microedition.location.version | 1.0 |
| 180 (SIP) | microedition.sip.version | 1.0 |
| 184 (M3G) | microedition.m3g.version | 1.0 |
| 185 (JTWI) | microedition.jtwi.version | 1.0 |
| 195 (IMP) | microedition.locale | 實現相關 |
| 195 | microedition.profiles | IMP-1.0 |
| 205 (WMA 2.0) | wireless.messaging.sms.smsc | 實現相關 |
| wireless.messaging.mms.mmsc | 實現相關 (彩信中心) |
運行時檢測代碼模板
String clcdVer = System.getProperty("microedition.configuration"); // CLDC-1.0/1.1
String midpVer = System.getProperty("microedition.profiles"); // MIDP-1.0/2.0
String jtwiVer = System.getProperty("microedition.jtwi.version"); // JTWI 支持?
String mmaVer = System.getProperty("microedition.mma.version"); // MMAPI 完整版?
String pimVer = System.getProperty("microedition.pim.version"); // JSR 75 PIM?
String locationVer = System.getProperty("microedition.location.version"); // JSR 179?
應用屬性:JAD 與 MANIFEST.MF 交互規則
必備屬性 (雙方必有,簽名 Suite 必須一致)
- MIDlet-Name
- MIDlet-Version
- MIDlet-Vendor
JAD 專有必備屬性
- MIDlet-Jar-URL (OTA 必須絕對路徑)
- MIDlet-Jar-Size (字節數)
MANIFEST 專有必備屬性 (JAD 可缺省)
- MIDlet-<n> (每個 MIDlet 描述)
- MicroEdition-Profile
- MicroEdition-Configuration
優先級邏輯 (MIDP 2.0 規範 §10.2.5)
// 可信任 Suite (已簽名並驗證)
if (trusted) {
v0 = MANIFEST[key]; v1 = JAD[key];
if (v0 != null && v1 != null) assert v0.equals(v1); // 關鍵屬性必須一致
value = (v1 != null) ? v1 : v0; // JAD 覆蓋非關鍵屬性
}
// 非可信任 Suite (未簽名)
else {
value = JAD[key]; // 優先 JAD
if (value == null) value = MANIFEST[key]; // 回退 MANIFEST
}
實戰:自定義屬性實現免重編譯配置
JAD 中定義
SERVER_URL: https://api.mycompany.com/v1/
MAX_RETRY: 3
TIMEOUT_MS: 5000
DEBUG_MODE: true
MIDlet 中讀取並類型轉換
public class Config {
private static Config instance;
private String serverUrl;
private int maxRetry;
private int timeoutMs;
private boolean debugMode;
private Config(MIDlet midlet) {
serverUrl = midlet.getAppProperty("SERVER_URL");
maxRetry = parseInt(midlet.getAppProperty("MAX_RETRY"), 3);
timeoutMs = parseInt(midlet.getAppProperty("TIMEOUT_MS"), 5000);
debugMode = "true".equalsIgnoreCase(midlet.getAppProperty("DEBUG_MODE"));
}
public static Config getInstance(MIDlet midlet) {
if (instance == null) instance = new Config(midlet);
return instance;
}
private int parseInt(String s, int def) {
try { return Integer.parseInt(s); } catch (Exception e) { return def; }
}
// getters...
}
典型 JAD 文件完整示例
MIDlet-Name: MyGame
MIDlet-Version: 1.2.0
MIDlet-Vendor: GameStudio Inc.
MIDlet-1: MyGame,/icon.png,com.gamestudio.MyGame
MIDlet-Jar-URL: https://cdn.gamestudio.com/MyGame.jar
MIDlet-Jar-Size: 45678
MicroEdition-Profile: MIDP-2.0
MicroEdition-Configuration: CLDC-1.1
MIDlet-Description: Awesome 2D Action Game
MIDlet-Icon: /icon.png
MIDlet-Info-URL: https://gamestudio.com/MyGame
MIDlet-Data-Size: 10240
MIDlet-Permissions: javax.microedition.io.Connector.http
MIDlet-Permissions-Opt: javax.microedition.io.Connector.socket
SERVER_URL: https://api.gamestudio.com/v1/
MAX_RETRY: 3
DEBUG_MODE: false
避坑指南
| 問題 | 原因 | 解決 |
| getAppProperty 返回 null | 屬性名大小寫不匹配 / JAD 未打包 / 簽名 Suite 關鍵屬性不一致導致安裝失敗 | 屬性區分大小寫;確保 JAD 隨 JAR 部署;簽名 Suite 嚴格對齊 3 大關鍵屬性 |
| 安裝提示「屬性不匹配 905」 | 簽名 Suite 的 JAD/MANIFEST 中 MIDlet-Name/Version/Vendor 不一致 | 打包腳本自動同步三大屬性 |
| JAD 過大導致安裝失敗 | 舊設備限制 JAD ≤ 5KB,自定義屬性過多 | 精簡自定義屬性,大量配置改用服務端下發 |
| OTA 下載 JAR 404 | MIDlet-Jar-URL 為相對路徑 | 必須使用絕對 URL (含域名/IP+端口+路徑) |
總結
系統屬性用於**運行時能力檢測** (CLDC/MIDP 版本、可選包支持),應用屬性用於**部署時配置外部化** (URL、超時、開關)。掌握 JAD/MANIFEST 優先級邏輯、簽名一致性要求、自定義屬性最佳實踐,是專業 MIDP 開發者的必備基本功。
- 上一篇文章: J2ME 調用 .NET Web Services 實戰:JSR 172 Stub 生成、異步調用與 kSOAP 替代方案對比
- 下一篇文章: J2ME 內存優化實戰寶典:手動釋放、對象復用、圖片優化、工具鏈與監測代碼模板
Copyright ©
程式碼之家 All Rights Reserved