如果有一個程序它在手機上一運行就知道這些配置的話,在某種程度上也方便了開發,其實要實現這個一點都不難,下面就是我寫的用于檢測手機的一些參數.程序里面只檢測了一部份,大家可以根據需要加上自己需要知道的內容.
首先是一個MIDlet,這是不能少的
/*
* Main.java
*
* Created on 2007-9-20, 15:08:53
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.hadeslee.phone;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* @author hadeslee
*/
public class Main extends MIDlet {
private InfoForm info;
public Main(){
initOther();
}
public Display getDisplay(){
return Display.getDisplay(this);
}
//初始化一些東西
private void initOther(){
info=new InfoForm(this);
}
public void startApp() {
getDisplay().setCurrent(info);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
* Main.java
*
* Created on 2007-9-20, 15:08:53
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.hadeslee.phone;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* @author hadeslee
*/
public class Main extends MIDlet {
private InfoForm info;
public Main(){
initOther();
}
public Display getDisplay(){
return Display.getDisplay(this);
}
//初始化一些東西
private void initOther(){
info=new InfoForm(this);
}
public void startApp() {
getDisplay().setCurrent(info);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
然后是包含了信息的一個Form
/*
* InfoForm.java
*
* Created on 2007-9-20, 15:10:02
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.hadeslee.phone;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.Ticker;
/**
*
* @author hadeslee
*/
public class InfoForm extends Form implements CommandListener {
private Main main;
private Command about;
private Command exit;
public InfoForm(Main main) {
super("配置表");
this.main=main;
initOther();
}
//初始化其它
private void initOther() {
about = new Command("關于", Command.OK, 1);
exit = new Command("退出", Command.EXIT, 1);
this.addCommand(about);
this.addCommand(exit);
this.setCommandListener(this);
this.setTicker(new Ticker("手機JAVA平臺參數查看"));
initStringItem();
}
//初始化字符串項
private void initStringItem() {
String[] keys = {"手機平臺", "手機編碼", "配置", "簡表", "地區", "主機名"};
String[] values = {"microedition.platform", "microedition.encoding", "microedition.configuration", "microedition.profiles", "microedition.locale", "microedition.hostname"};
for (int i = 0; i < keys.length; i++) {
StringItem item = new StringItem(keys[i], System.getProperty(values[i]));
this.append(item);
}
}
public void commandAction(Command c, Displayable d) {
if (c == about) {
main.getDisplay().setCurrent(new Alert("關于\n",
"查看手機的一些JAVA配置\n作者:千里冰封",null,AlertType.INFO), this);
} else if (c == exit) {
main.notifyDestroyed();
main.destroyApp(true);
}
}
}
* InfoForm.java
*
* Created on 2007-9-20, 15:10:02
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.hadeslee.phone;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.Ticker;
/**
*
* @author hadeslee
*/
public class InfoForm extends Form implements CommandListener {
private Main main;
private Command about;
private Command exit;
public InfoForm(Main main) {
super("配置表");
this.main=main;
initOther();
}
//初始化其它
private void initOther() {
about = new Command("關于", Command.OK, 1);
exit = new Command("退出", Command.EXIT, 1);
this.addCommand(about);
this.addCommand(exit);
this.setCommandListener(this);
this.setTicker(new Ticker("手機JAVA平臺參數查看"));
initStringItem();
}
//初始化字符串項
private void initStringItem() {
String[] keys = {"手機平臺", "手機編碼", "配置", "簡表", "地區", "主機名"};
String[] values = {"microedition.platform", "microedition.encoding", "microedition.configuration", "microedition.profiles", "microedition.locale", "microedition.hostname"};
for (int i = 0; i < keys.length; i++) {
StringItem item = new StringItem(keys[i], System.getProperty(values[i]));
this.append(item);
}
}
public void commandAction(Command c, Displayable d) {
if (c == about) {
main.getDisplay().setCurrent(new Alert("關于\n",
"查看手機的一些JAVA配置\n作者:千里冰封",null,AlertType.INFO), this);
} else if (c == exit) {
main.notifyDestroyed();
main.destroyApp(true);
}
}
}
在這里我只列舉出來了"microedition.platform", "microedition.encoding", "microedition.configuration", "microedition.profiles", "microedition.locale", "microedition.hostname"的信息,其實還有很多別的信息是可以知道的,下面是所有可查詢的信息列表:
JSR | Property Name |
Default Value¹ |
---|---|---|
30 | microedition.platform | null |
microedition.encoding | ISO8859_1 | |
microedition.configuration | CLDC-1.0 | |
microedition.profiles | null | |
37 | microedition.locale | null |
microedition.profiles | MIDP-1.0 | |
75 | microedition.io.file.FileConnection.version | 1.0 |
file.separator | (impl-dep) | |
microedition.pim.version | 1.0 | |
118 | microedition.locale | null |
microedition.profiles | MIDP-2.0 | |
microedition.commports | (impl-dep) | |
microedition.hostname | (impl-dep) | |
120 | wireless.messaging.sms.smsc | (impl-dep) |
139 | microedition.platform | (impl-dep) |
microedition.encoding | ISO8859-1 | |
microedition.configuration | CLDC-1.1 | |
microedition.profiles | (impl-dep) | |
177 | microedition.smartcardslots | (impl-dep) |
179 | microedition.location.version | 1.0 |
180 | microedition.sip.version | 1.0 |
184 | microedition.m3g.version | 1.0 |
185 | microedition.jtwi.version | 1.0 |
195 | microedition.locale | (impl-dep) |
microedition.profiles | IMP-1.0 | |
205 | wireless.messaging.sms.smsc | (impl-dep) |
205 | wireless.messaging.mms.mmsc | (impl-dep) |
不能保證以上都有效,因為手機支持度不一樣,從JSR30到JSR205都有不同的參數可以讓我們知道.
另外我們也可以通過MIDlet的getAppProperty(String key)來得到我們定義在JAD文件里面的一些參數,這樣就便于我們發布程序了.
下面是以上例子的NetBeans工程文件,點擊下載. 如果想直接能運行的話,可以把dist目錄下面的兩個文件拷到手機上,就可以運行了,運行環境是最低的MIDP1.0和CLDC1.0
盡管千里冰封
依然擁有晴空
你我共同品味JAVA的濃香.