查看手机对JAVA的支持信息

时间:2008-07-03 02:23:57  类别:手机java  作者:秋风

我们在开发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"的信息,其实还有很多别的信息是可以知道的,下面是所有可查询的信息列表:
JSRProperty Name
Default Value
30microedition.platformnull
microedition.encodingISO8859_1
microedition.configurationCLDC-1.0
microedition.profilesnull
37microedition.localenull
microedition.profilesMIDP-1.0
75microedition.io.file.FileConnection.version1.0
file.separator(impl-dep)
microedition.pim.version1.0
118microedition.localenull
microedition.profilesMIDP-2.0
microedition.commports(impl-dep)
microedition.hostname(impl-dep)
120wireless.messaging.sms.smsc(impl-dep)
139microedition.platform(impl-dep)
microedition.encodingISO8859-1
microedition.configurationCLDC-1.1
microedition.profiles(impl-dep)
177microedition.smartcardslots(impl-dep)
179microedition.location.version1.0
180microedition.sip.version1.0
184microedition.m3g.version1.0
185microedition.jtwi.version1.0
195microedition.locale(impl-dep)
microedition.profilesIMP-1.0
205wireless.messaging.sms.smsc(impl-dep)
205wireless.messaging.mms.mmsc(impl-dep)

不能保证以上都有效,因为手机支持度不一样,从JSR30到JSR205都有不同的参数可以让我们知道.
另外我们也可以通过MIDlet的getAppProperty(String key)来得到我们定义在JAD文件里面的一些参数,这样就便于我们发布程序了.
下面是以上例子的NetBeans工程文件,点击下载. 如果想直接能运行的话,可以把dist目录下面的两个文件拷到手机上,就可以运行了,运行环境是最低的MIDP1.0和CLDC1.0


特别推荐

广而告之