在 J2ME 開發中,獲取手機的 IMEI(國際移動設備識別碼)和 Cell ID(基站識別碼)是常見需求,但由於這兩個屬性屬於廠商擴展實現,並無統一標準 API。本文整理了主流手機廠商的系統屬性鍵值,並提供可直接用於 J2ME Polish 適配框架的測試代碼,供開發者參考與補充。
package study;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import de.enough.polish.ui.Form;
public class GetIMEIAndCellId extends MIDlet implements CommandListener {
private Command exitCommand = new Command("exit", Command.EXIT, 1);
private Form form = new Form("IMEI and Cell ID");
private Display display = null;
public GetIMEIAndCellId() {
display = Display.getDisplay(this);
}
protected void destroyApp(boolean unconditional) {}
protected void pauseApp() {}
protected void startApp() throws MIDletStateChangeException {
String platform = System.getProperty("microedition.platform");
String imei = "";
String cellid = "";
String lac = "";
// Sony Ericsson
//#if polish.vendor==Sony-Ericsson
imei = System.getProperty("com.sonyericsson.imei");
cellid = System.getProperty("com.sonyericsson.net.cellid");
lac = System.getProperty("com.sonyericsson.net.lac");
//#elif polish.vendor==Nokia
imei = System.getProperty("phone.imei");
if (imei == null || imei.isEmpty()) {
imei = System.getProperty("com.nokia.IMEI");
}
if (imei == null || imei.isEmpty()) {
imei = System.getProperty("com.nokia.mid.imei");
}
//#if polish.group==Series60
cellid = System.getProperty("com.nokia.mid.cellid");
//#elif polish.group==Series40
cellid = System.getProperty("Cell-ID");
//#endif
//#elif polish.vendor==Siemens
imei = System.getProperty("com.siemens.imei");
//#elif polish.vendor==Motorola
imei = System.getProperty("com.motorola.IMEI");
cellid = System.getProperty("CellID");
//#elif polish.vendor==Samsung
imei = System.getProperty("com.samsung.imei");
//#endif
if (imei == null || imei.isEmpty()) {
imei = System.getProperty("IMEI");
}
form.append("Platform: " + platform);
form.append("IMEI: " + imei);
form.append("Cell ID: " + cellid);
form.setCommandListener(this);
form.addCommand(exitCommand);
display.setCurrent(form);
}
public void commandAction(Command cmd, Displayable item) {
if (cmd == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
}
由於廠商實現差異極大,建議採用「嘗試多組屬性鍵值、優先級排序」的策略。實際部署時請在目標機型上實機測試,並歡迎社群補充更多機型的測試結果。