在Java ME中取得Cell ID

这一篇是我在CS000947 – Getting Cell ID in Java ME的中文翻译,网址在在Java ME中取得Cell ID
为了备份,并转贴在此:

ID CS000947 Creation date May 14, 2008
Platform Series 40 3rd Edition, FP1 and S60 3rd Edition, FP2 Tested on devices
Category Java Subcategory MIDP 2.0
Keywords (APIs, classes, methods, functions): System.getProperty(), Nokia proprietary system properties

概论

这个程式码描述在S40及S60系列的机器上如何使用Nokia-proprietary的系统属性取得手机的cell ID,注意在不同系列的机器上需使用不同的系统属性:

Series 40 3rd Edition, FP1 (或较新的):

System.getProperty("Cell-ID")

S60 3rd Edition, FP2 (或较新的):

System.getProperty("com.nokia.mid.cellid")

注意: 在S40机器上MIDlet需要制造商或运销区域的签名,否则这个属性会是空值,S60的机上不需要签名。

原始码

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class CellIDMIDlet extends MIDlet implements CommandListener {
    private Form form;
    private Command exitCommand;
    private String S40_cell_id;   // Series 40 cell id property
    private String S60_cell_id;   // S60 cell id property

    public void startApp() {
        form = new Form("Getting Cell ID");
        S40_cell_id = System.getProperty("Cell-ID");
        S60_cell_id = System.getProperty("com.nokia.mid.cellid");
        form.append("Series 40 devices: " + S40_cell_id + "\n");
        form.append("S60 devices: " + S60_cell_id);
        exitCommand = new Command("Exit", Command.EXIT, 1);
        form.setCommandListener(this);
        form.addCommand(exitCommand);
        Display.getDisplay(this).setCurrent(form);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) this.notifyDestroyed();
    }
}

后处理

当MIDlet在S40或S60系列的机器上执行时,cell ID会显示在表单上(一个有数值另一个有’null’)。

另见

Java ME系统属性