首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> J2ME

MIDP 應用屬性機制權威指南:系統屬性表、JAD/MANIFEST 優先級、getAppProperty 最佳實踐

     編輯:J2ME

兩類屬性來源

  • 系統屬性:由 CLDC/MIDP/可選包規範定義,System.getProperty(key) 讀取,只讀
  • 應用屬性:定義於 JAD 或 MANIFEST.MF,MIDlet.getAppProperty(key) 讀取,部署時可配置

完整系統屬性參考表 (按 JSR 分組)

JSR屬性名默認值/說明
30 (CLDC 1.0)microedition.platformnull (設備平台名)
microedition.encodingISO8859_1 (默認字符編碼)
microedition.configurationCLDC-1.0
microedition.profilesnull
microedition.localenull (當前語言環境)
37 (MIDP 1.0)microedition.profilesMIDP-1.0
75 (PIM/File)microedition.io.file.FileConnection.version1.0
75file.separator實現相關 (如 "/")
75microedition.pim.version1.0
118 (MIDP 2.0)microedition.localenull
microedition.profilesMIDP-2.0
microedition.commports實現相關 (串口列表)
microedition.hostname實現相關 (設備主機名)
120 (WMA 1.1)wireless.messaging.sms.smsc實現相關 (短信中心號碼)
139 (CLDC 1.1)microedition.platform實現相關
microedition.encodingISO8859-1
microedition.configurationCLDC-1.1
microedition.profiles實現相關
177 (SATSA)microedition.smartcardslots實現相關 (智能卡槽數)
179 (Location)microedition.location.version1.0
180 (SIP)microedition.sip.version1.0
184 (M3G)microedition.m3g.version1.0
185 (JTWI)microedition.jtwi.version1.0
195 (IMP)microedition.locale實現相關
195microedition.profilesIMP-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

避坑指南

總結

系統屬性用於**運行時能力檢測** (CLDC/MIDP 版本、可選包支持),應用屬性用於**部署時配置外部化** (URL、超時、開關)。掌握 JAD/MANIFEST 優先級邏輯、簽名一致性要求、自定義屬性最佳實踐,是專業 MIDP 開發者的必備基本功。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
問題原因解決
getAppProperty 返回 null屬性名大小寫不匹配 / JAD 未打包 / 簽名 Suite 關鍵屬性不一致導致安裝失敗屬性區分大小寫;確保 JAD 隨 JAR 部署;簽名 Suite 嚴格對齊 3 大關鍵屬性
安裝提示「屬性不匹配 905」簽名 Suite 的 JAD/MANIFEST 中 MIDlet-Name/Version/Vendor 不一致打包腳本自動同步三大屬性
JAD 過大導致安裝失敗舊設備限制 JAD ≤ 5KB,自定義屬性過多精簡自定義屬性,大量配置改用服務端下發
OTA 下載 JAR 404MIDlet-Jar-URL 為相對路徑必須使用絕對 URL (含域名/IP+端口+路徑)