19
Thomas Wiradikusuma [email protected] www.wiradikusuma.com Presentation to the 19 th Java User Group Meet Up (JaMU), March 4, 2006 A Taste of A Taste of JavaME JavaME Overview to Java’s approach to wireless mobile Overview to Java’s approach to wireless mobile application development from a developer’s application development from a developer’s perspective perspective

A Taste of Java ME

Embed Size (px)

Citation preview

Page 1: A Taste of Java ME

Thomas [email protected]

www.wiradikusuma.com

Presentation to the 19th Java User Group Meet Up (JaMU), March 4, 2006

A Taste of JavaMEA Taste of JavaMEOverview to Java’s approach to wireless mobile application Overview to Java’s approach to wireless mobile application

development from a developer’s perspectivedevelopment from a developer’s perspective

Page 2: A Taste of Java ME

ObjectiveObjective

To introduce JavaME as a platform for To introduce JavaME as a platform for developing wireless mobile developing wireless mobile applications.applications.

Page 3: A Taste of Java ME

AgendaAgenda

• Wireless mobile environmentWireless mobile environment• JavaME architectureJavaME architecture• JavaME versus JavaSEJavaME versus JavaSE• First MIDletFirst MIDlet• User interface and event handlingUser interface and event handling• PersistencePersistence• NetworkingNetworking• Game APIGame API• Extensions (Messaging, Multimedia, Bluetooth, Extensions (Messaging, Multimedia, Bluetooth,

etc)etc)• PerformancePerformance

Page 4: A Taste of Java ME

World of Wireless MobileWorld of Wireless Mobile

• Wireless is in the air.Wireless is in the air.• Wireless communication is one of the Wireless communication is one of the

fastest growing technology.fastest growing technology.• Cell phones subscribers worldwide Cell phones subscribers worldwide

exceeded two billions in 2005. exceeded two billions in 2005. • More depended to the Internet, people More depended to the Internet, people

want it accessible from mobile devices. want it accessible from mobile devices. • 3+G is coming to town.3+G is coming to town.

Page 5: A Taste of Java ME

Meet JavaMEMeet JavaME

• JavaME definedJavaME defined• A little historyA little history• Why JavaME?Why JavaME?

– Dynamic delivery of contentDynamic delivery of content– Offline access Offline access – Enhanced user experience and interactive content Enhanced user experience and interactive content – Cross-platform compatibilityCross-platform compatibility– Security Security – The power of a modern object-oriented programming The power of a modern object-oriented programming

language language – Large developer community Large developer community

Page 6: A Taste of Java ME

JavaME ArchitectureJavaME Architecture

Page 7: A Taste of Java ME

JavaME Architecture, cont’dJavaME Architecture, cont’d

• Configurations:Configurations:– Connected Device ConfigurationConnected Device Configuration– Connected, Limited Device ConfigurationConnected, Limited Device Configuration

• Profiles:Profiles:– Mobile Information Device Profile Mobile Information Device Profile – PDA ProfilePDA Profile– Foundation ProfileFoundation Profile– etcetc

• Optional packagesOptional packagesTypically contains functionality independent of any Typically contains functionality independent of any particular vertical market segment or device family. The particular vertical market segment or device family. The main goal is to allow the definition of APIs that can be main goal is to allow the definition of APIs that can be added flexibly on top of a number of different profiles. One added flexibly on top of a number of different profiles. One device can support multiple optional packages.device can support multiple optional packages.

Page 8: A Taste of Java ME

Miniaturized Standard Java Miniaturized Standard Java APIsAPIs• No floating-point in CLDC 1.0.No floating-point in CLDC 1.0.• No user class loading, object finalization, No user class loading, object finalization,

reflection, native methods, demon threads, reflection, native methods, demon threads, thread groups, Collections API, etc.thread groups, Collections API, etc.

• Simpler Runtime and System, java.io, date Simpler Runtime and System, java.io, date functions, exception handling, etc.functions, exception handling, etc.

• TIPS: TIPS: – Always refer to Javadoc.Always refer to Javadoc.– When compiling, set Language Features and When compiling, set Language Features and

Target VM to lowest Java SDK available (IDEs Target VM to lowest Java SDK available (IDEs can help this).can help this).

Page 9: A Taste of Java ME

Developing MIDletDeveloping MIDlet

• Edit Source Code > Compile > Preverify > Package > Test or Edit Source Code > Compile > Preverify > Package > Test or Deploy.Deploy.

• Free development tools: Free development tools: – J2ME Wireless Toolkit J2ME Wireless Toolkit – EclipseME (an Eclipse plug-in)EclipseME (an Eclipse plug-in)– NetBeans Mobility Pack (a NetBeans module)NetBeans Mobility Pack (a NetBeans module)

• Obfuscation: minimizing the size of MIDlet suite JARs. Obfuscation: minimizing the size of MIDlet suite JARs. Originally designed to foil attempts to reverse engineer Originally designed to foil attempts to reverse engineer compiled bytecode, perform any combination of the following compiled bytecode, perform any combination of the following functions:functions:– Renaming things to make them compactRenaming things to make them compact– Removing unused thingsRemoving unused things– Inserting illegal or questionable data to confuse decompilers Inserting illegal or questionable data to confuse decompilers

• Other useful tools: Other useful tools: – Antenna (specialized Antenna (specialized Ant tasks to help with MIDlet suite builds)Ant tasks to help with MIDlet suite builds)– J2ME PolishJ2ME Polish

• Test your application in (varying) real devices as often as Test your application in (varying) real devices as often as possible.possible.

Page 10: A Taste of Java ME

First MIDlet (“Hello World”)First MIDlet (“Hello World”)

Page 11: A Taste of Java ME

User InterfaceUser Interface

Page 12: A Taste of Java ME

Event HandlingEvent Handling

• A command is something the user can A command is something the user can invoke—you can think of it as a button. Like invoke—you can think of it as a button. Like a button, it has a title, like “OK” or a button, it has a title, like “OK” or “Cancel,” and your application can respond “Cancel,” and your application can respond appropriately when the user invokes the appropriately when the user invokes the command. The premise is that you want a command. The premise is that you want a command to be available to the user, but command to be available to the user, but you don't really care how it is shown on the you don't really care how it is shown on the screen or exactly how the user invokes it—screen or exactly how the user invokes it—keypad button, soft button, touch screen, keypad button, soft button, touch screen, whatever.whatever.

Page 13: A Taste of Java ME

Persisting DataPersisting Data

““Record stores”, small database containing Record stores”, small database containing pieces of data called pieces of data called recordsrecords. Represented . Represented by instances of by instances of javax.microedition.rms.RecordStore. javax.microedition.rms.RecordStore.

Page 14: A Taste of Java ME

Connecting to the World Connecting to the World • All contained in the javax.microedition.io package and All contained in the javax.microedition.io package and

based around the Connection interface. based around the Connection interface.

• HTTP (and HTTPS in MIDP 2.0) is mandatory, others are HTTP (and HTTPS in MIDP 2.0) is mandatory, others are optional.optional.

Page 15: A Taste of Java ME

Connecting to the World, Connecting to the World, cont’dcont’d

• TIPS:TIPS:– Use GET rather than POST. Use GET rather than POST. – Don't hard-code URLs.Don't hard-code URLs.– Put network access in a separate thread. Put network access in a separate thread. – Handle exceptions gracefully. Handle exceptions gracefully. – Clean up after yourself. Clean up after yourself.

Page 16: A Taste of Java ME

Game APIGame API

• Simplifies writing 2D games.Simplifies writing 2D games.• GameCanvas’ advantages over Canvas:GameCanvas’ advantages over Canvas:

– Controls exactly Controls exactly whenwhen the display is updated the display is updated– Controls region of the screen to updateControls region of the screen to update

• Typical game loop:Typical game loop:Graphics g = getGraphics(); Graphics g = getGraphics(); while(true) { while(true) { // Check for user input. // Check for user input. // Update game state. // Update game state. // Draw stuff using g. // Draw stuff using g. flushGraphics(); flushGraphics(); } }

• Layer, Sprite, TiledLayer and LayerManagerLayer, Sprite, TiledLayer and LayerManager• Special effects:Special effects:

– flashBacklight()flashBacklight()– vibrate()vibrate()

Page 17: A Taste of Java ME

Optional PackagesOptional Packages

• JSR 120: Wireless Messaging APIJSR 120: Wireless Messaging API• JSR 135: Mobile Media APIJSR 135: Mobile Media API• JSR 172: Web Services SpecificationJSR 172: Web Services Specification• JSR 177: Security and Trust ServicesJSR 177: Security and Trust Services• JSR 179: Location APIJSR 179: Location API• JSR 180: Session Initiation Protocol (SIP)JSR 180: Session Initiation Protocol (SIP)• JSR 184: Mobile 3D GraphicsJSR 184: Mobile 3D Graphics• JSR 190: Event Tracking APIJSR 190: Event Tracking API• And a whole lot more…And a whole lot more…

Page 18: A Taste of Java ME

Performance IssuesPerformance Issues

• BenchmarkingBenchmarking– Runtime.freeMemory() Runtime.freeMemory() – Runtime.totalMemory() Runtime.totalMemory() – System.currentTimeMilis()System.currentTimeMilis()– Run multiple times and calculate the averageRun multiple times and calculate the average

• TIPS:TIPS:– First make it run, the make it run right, then make it run fastFirst make it run, the make it run right, then make it run fast– Create objects efficiently, reuse objects whenever possibleCreate objects efficiently, reuse objects whenever possible– Use Use StringBuffer for string concatenationStringBuffer for string concatenation– Use arrays, not object collectionUse arrays, not object collection– Use buffered I/OUse buffered I/O– Be cleanBe clean– Make application responsive (“perceived performance”)Make application responsive (“perceived performance”)– Use an obfuscator Use an obfuscator – Redesign / refactorRedesign / refactor– Run memory profilerRun memory profiler– Manage resources efficientlyManage resources efficiently– Don’t be too OO :-)Don’t be too OO :-)

Page 19: A Taste of Java ME

ResourcesResources

• Websites:Websites:– wireless.java.sun.comwireless.java.sun.com– www.microjava.comwww.microjava.com– www.onjava.com/onjava/wirelesswww.onjava.com/onjava/wireless– www.javagaming.comwww.javagaming.com– www.microdevnet.comwww.microdevnet.com

• Books:Books:– Knudsen, Jonathan. Knudsen, Jonathan. Wireless Java Developing with J2ME, Wireless Java Developing with J2ME,

Second EditionSecond Edition. Apress. 2003.. Apress. 2003.– Riggs, Roger, Riggs, Roger, et alet al. . Programming Wireless Devices with Programming Wireless Devices with

the the Java™ 2 Platform, Micro Edition, Second EditionJava™ 2 Platform, Micro Edition, Second Edition. . Addison Addison Wesley. 2003.Wesley. 2003.