我们在开发J2ME的时候,都需要知道手机到底支持JAVA到哪种程度,比如CLDC1.0还是CLDC1.1对于简表的支持是MIDP1.0还是MIDP2.0或者是最新的MIDP2.1.
如果有一个程序它在手机上一运行就知道这些配置的话,在某种程度上也方便了开发,其实要实现这个一点都不难,下面就是我写的用于检测手机的一些参数.程序里面只检测了一部份,大家可以根据需要加上自己需要知道的内容.
首先是一个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) {
}
}
然后是包含了信息的一个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);
}
}
}
在这里我只列举出来了"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的浓香.
posted on 2007-09-20 16:19
千里冰封 阅读(1188)
评论(3) 编辑 收藏 所属分类:
JAVAME