0% found this document useful (0 votes)
26 views4 pages

MIT AITI Mobile Application Development in Java Lab 15: J2ME Introduction

This document provides an overview of developing a mobile weather application in Java ME (J2ME). It describes creating a basic "Hello World" application using the Wireless Toolkit to test it in an emulator. It then provides steps to create a more involved user interface with options to select a city, submit a custom query, and display weather results. The steps include initializing UI components, handling commands, making a weather query, and displaying results.

Uploaded by

Wallace Mureithi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

MIT AITI Mobile Application Development in Java Lab 15: J2ME Introduction

This document provides an overview of developing a mobile weather application in Java ME (J2ME). It describes creating a basic "Hello World" application using the Wireless Toolkit to test it in an emulator. It then provides steps to create a more involved user interface with options to select a city, submit a custom query, and display weather results. The steps include initializing UI components, handling commands, making a weather query, and displaying results.

Uploaded by

Wallace Mureithi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MITAITIMobileApplicationDevelopmentinJava Lab15:J2MEIntroduction

ThislabwillgiveyouanintroductiontoJ2MEprogramming.Specifically,youwilllearntousetheSun JavaWirelessToolkit(WTK)tocompileyourcodeanduseanemulatortotestit.Whilethislabismostly justfollowingdirections,itisimportanttopayattentiontoandthinkabouteachstepinorderto understandtheJ2MEdevelopmentprocess.TheJavaMicroEditionissimilartoJ2SE,buttherearekey differencesinthecompilationprocessandtheincludedlibraries.

HelloWorld on the Cell Phone


BeginbyopeningWirelessToolkit3.0.0,undertheSunJavaWirelessToolkit.SelecttheFile>New Project(ortheNewProjectbutton).MakesureMIDPApplicationisselectedandthenhitNext>. Selectaprojectname(Lab14,forexample).MakesuretheCreateHelloMIDletboxischecked.Click Next>andthenFinish.ClickRuntoexecutethecode. Afterrunningthemobilephoneemulator,examinethecode.Noticethiscodehasthebareminimum requirementsforacellphoneapplication:importedmidletandlcduipackages,extendingtheMIDlet class,aconstructor,astartApp()method,apauseApp()method,andadestroyApp() method.Theimportedpackagescontainbasiclibraryclassesthatwillbeusedinacellphone application.MIDletisanabstractclassinthemidletpackagethatrequiresimplementationofthe startApp(),pauseApp()anddestroyApp()methodsinanonabstractsubclass.The constructoriscalleduponcreatingtheapplicationandtheMIDPimplementationinitiatesthe applicationviathestartApp()method.ThepauseApp()anddestroyApp()methodsdoas theirnamessuggest,butatthispointhavebeengivenemptyimplementations.

Documentation
AssumingyourWTKislocatedatC:\Java_ME_platform_SDK_3.0,youcanfindtheJ2MEdocumentation at:C:\Java_ME_platform_SDK_3.0\docs.TheMIDPdocumentationisrootedat: C:\Java_ME_platform_SDK_3.0\docs\api\midp2.0\index.html. MITAfricaInformationTechnologyInitiative 1

A more involved example


Inthisexample,youwillwriteaJ2MEuserinterfacethatmightbeusedwithyourweatherapplication. YouwillnotactuallyrewriteyourweatherapplicationstobecompatiblewithJ2ME,butthiswillgive youpracticewithbothJ2MEanduserinterfaces. TheopeningUIwillhaveaChoiceGroupwhichwillcontainafewoptionsfortheusertoselectfrom (forexampleChicago,USAorNairobi,Kenya).TherewillalsobetwoTextFieldobjects:onefor theusertosubmitacitynameandonetosubmitacountryname.ThesewillallbecontainedinaForm object.SeeFigure1.

Figure1:FirstScreen

Figure2:SecondScreen

TheapplicationwillhavetwoCommands:aquerycommandandanexitcommand.TheFormwill havebothcommands.Whenquerycommandisselected,yourapplicationwillfakeaweathersearch. Fornow,justhavethesearchreturnsomemadeupweatherdataforthequery.Forexample,the queryNairobi,KenyamightreturnThetemperatureis17Candthehumidityis68%inNairobi, Kenya. DisplaythisresultinaTextBox(afterqueryisselected).TheTextBoxwillalsohavetheexit command.SeeFigure2. 1. Rightclickonthepackage(hello)andselectNew>MIDletSelectaname,thenclickFinish.

MITAfricaInformationTechnologyInitiative

2. Inyourconstructor,initializealltheobjectsyouwillneed(oneChoiceGroup,two TextFields,oneForm,oneTextBox,twoCommands,andoneDisplayobject).Hereare somesampleconstructions: queryCommand = new Command("[command name]",[Command type],0); //dont worry too much about the last two arguments //Command.EXIT or Command.OK are two options for Command types display = Display.getDisplay(this); submitter = new TextField("[title]","[initial text]", [max length],TextField.ANY); //TextField.ANY means any characters can be typed theItems = new ChoiceGroup("[title]",ChoiceGroup.EXCLUSIVE); //EXCLUSIVE means only one option may be selected at once theItems.append("Nairobi, Kenya",null); //The null refers to the image for the entry, but we wont have //any images results = new TextBox("[title]","[initial text]", [max length],TextField.ANY); 3. UsetheaddCommand()andappend()methodstocompletethesetup.The ChoiceGroupandTextFieldsmustbeappend()edtotheForminordertobe displayed.Wellgetintothatmorelater,butessentiallyChoiceGroupandTextFieldare setuptobedisplayedinaformalongwithmanyotherItems. BothCommandsshouldalsobeaddedtotheForm.Theexitcommandshouldalsobeaddedto theTextBox. 4. HaveyourMIDletimplementtheCommandListenerinterface.Thismeansthatitshould haveavoid commandAction(Command [variable name],Displayable [variable name])method. UsesetCommandListener(this)tosetthecurrentinstanceofyourMIDletasthe commandlistenerforyourFormandTextBox.Thismeansthatwheneveracommandis calledfromeitheroftheseobjects,thecommandAction()methodofyourMIDletwillbe called. WewillfillinthecommandAction()methodinstep6. 5. InthestartApp()method,useTextFieldssetString()toresettheTextFieldss initialtextblank.ThenuseDisplayssetCurrent()methodtodisplaytheForm.

MITAfricaInformationTechnologyInitiative

6. NowwewillwritethecommandAction()methodofourMIDlet.IftheCommandargument isyourexitCommand,quittheprogram.DothisbycallingdestroyApp(true) (unnecessaryhere,butgoodstyle)andthennotifyDestroyed()(actuallyquitsthe MIDlet). IftheCommandisyourqueryCommand,therewillbetwocases: a) Thefirstcaseiswherenocityhasbeenselectedfromthelist.Determinethisusing ChoiceGroupsgetSelectedIndex()method.Itshouldreturn1ifnothingis selected. Inthiscase,callyourgetWeather()functionwiththequery[cityname]+,+[country name].Savethisasyourresult.UseTextFieldsgetString()methodtofindout whattheuserhastypedintotheTextFields. b) Thesecondcaseiswhereacityhasbeenselectedfromthelist.UseChoiceGroups getString(int)todeterminewhichcitywasselected.CallyourgetWeather() methodonthisStringandsavetheoutputasyourresult. 7. ChangetheTextBoxsvaluetoyourresultwiththesetString()method.Thenchange yourDisplay(usesetCurrent())totheTextBox. 8. Runyourprogramtotestit.IfthisisyourfirstJ2MEapplication,feelproud.

MITAfricaInformationTechnologyInitiative

You might also like