Java ME/J2ME 环境搭建、WM6.1运行JBED虚拟机手机实现JSR179定位
接触Java ME,到现在已经过去五天了。利用一款带GPS模块的WM6.1系统的手机实现定位和数据上报项目,周期为18天,公司没有一个人搞过手机开发,囧,身为研发部负责人责无旁贷(目前是光杆司令)……
废话不多讲,刚开始用MyEclispe + MyEclispeMe + WTK + JDK 搭建了开发环境(这也是费了九牛二虎之力)
一、Java ME/J2ME环境的搭建
1、安装 MyEclipse
MyEclipse_6.5.1GA_E3.3.2_Installer.exe、myEclipse6.5汉化包.rar、MyEclipse注册码.txt
2、安装 MyEclispeMe 开发插件
运行MyEclispe –>帮助–>Software Updates–>Find and Install –>搜索新部件
新建站点:http://eclipseme.org/updates/ (可能要翻翻哦),确定,全部勾选,安装,完毕。
3、安装 JDK
JDK6.0.21版本,如果找不到就先安装 jdk-6u20-windows-i586.exe 再打21补丁 JavaSetup6u21.exe。
4、安装 JavaME SDK
sun_java_me_sdk-3_0-win.exe
5、安装 WTK
WTK2.5.2_01版本,sun_java_wireless_toolkit-2.5.2_01-win.exe
MyEclispe–>首选项–>J2ME–>设备管理–>选择WTK目录–>refresh
(以上这些软件你就百度吧,不行就google,再不行必应)
注意:MyEclipse 编译的JAR包,很可能在虚拟机上安装不了。提示什么907啊,30的错误。
解决办法就是打开,JAR,编辑META-INF文件夹下的 MANIFEST.MF 文件,增加一行:
MIDlet-1: Test,,demo.LocationMIDlet 名字为Test,入口类为demo.LocationMIDlet
有的环境可能需要JAD包,相应的里面也要加上这一行。
二、支持JSR179的虚拟机
WM6.1上安装4EsmertecJbed_v20090217.5.1a_Chs.cab,狗狗搜,搜不到就找我要吧。
三、测试代码
package demo; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.List; import javax.microedition.location.Coordinates; import javax.microedition.location.Criteria; import javax.microedition.location.Location; import javax.microedition.location.LocationException; import javax.microedition.location.LocationProvider; //import javax.microedition.location.QualifiedCoordinates; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class LocationMIDlet extends MIDlet implements CommandListener, Runnable { private Display myDisplay; private Command okcmd; private Command ecmd; private Form form; private List myList; private boolean yes; public LocationMIDlet() { myDisplay = Display.getDisplay(this); myList = new List("测试真机是否支持 JSR179", List.IMPLICIT); okcmd = new Command("测试", Command.OK, 1); ecmd = new Command("退出", Command.OK, 1); form = new Form("GPS"); String cellid=System.getProperty("CellID"); if(cellid==null||"".equals(cellid)){ cellid=System.getProperty("Cell-ID"); } if(cellid==null||"".equals(cellid)){ cellid=System.getProperty("CELLID"); } if(cellid==null||"".equals(cellid)){ cellid=System.getProperty("CELL-ID"); } form.append("CellID:"+cellid); form.append("JSR:"+System.getProperty("Version")); myList.addCommand(okcmd); form.addCommand(ecmd); form.setCommandListener(this); myList.setCommandListener(this); } protected void destroyApp(boolean arg0) throws MIDletStateChangeException {} protected void pauseApp(){} protected void startApp() throws MIDletStateChangeException { myDisplay.setCurrent(myList); } public void commandAction(Command arg0, Displayable arg1) { if(arg0 == okcmd) { String version = System.getProperty("microedition.location.version"); yes = (version != null && !version.equals("")); Thread t = new Thread(this); t.start(); } if(arg0 == ecmd) { this.notifyDestroyed(); } } public void run() { myDisplay.setCurrent(form); //测试真机是否支持 jsr179 if(yes) { // Set criteria for selecting a location provider: // accurate to 500 meters horizontally // 设置精度 Criteria myCriteria = new Criteria(); myCriteria.setHorizontalAccuracy(500); double lat = 0; double lon = 0; // Get an instance of the provider // 找卫星,找服务 try { LocationProvider myLocationProvider = LocationProvider.getInstance(myCriteria); // Request the location, setting a one-minute timeout // 请求位置,并设置超时时间 Location myLocation = myLocationProvider.getLocation(5000); Thread.sleep(1000); Coordinates myCoordinates = myLocation.getQualifiedCoordinates(); for(int i=0;i<10;i++){ if(myCoordinates != null) { // Use coordinate information // 得到经纬度 lat = myCoordinates.getLatitude(); lon = myCoordinates.getLongitude(); } form.append("真机支持JSR179,纬度坐标:" + String.valueOf(lat) + ",经度坐标:" + String.valueOf(lon)); form.append("------------"+i); try{ Thread.sleep(1000); }catch(Exception e){ } } } catch (LocationException e) { form.append("LocationException 发生异常"); e.printStackTrace(); } catch (InterruptedException e) { form.append("InterruptedException 发生异常"); e.printStackTrace(); } } //真机不支持 jsr179 else { form.append("真机不支持JSR179"); } } }