Emerging Technology
Fall Semester, 2004 J2ME Intro |
||
---|---|---|
© 2004, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 12-Oct-04 |
CS 683 Emerging Technologies Fall Semester, 2004 Doc 18 J2ME Intro
Connected Limited Device Configuration (CLDC)
Running the Example the Hard Way
Running the Example the Easy Way
javax.microedition.midlet.MIDlet & MIDlet States
Copyright ©, All rights reserved. 2004 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent ( http://www.opencontent.org/opl.shtml) license defines the copyright on this document.
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 2 |
J2ME in a Nutshell, Kim Topley, O’Reilly, 2002
Sun’s J2ME Wireless Toolkit Documentation
Documentation in J2ME Wireless Toolkit download
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 3 |
Download at:
User’s Guide
Sun’s J2ME Documentation Page
J2ME API Documentation
For the course J2ME Wireless Toolkit documentation:
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 4 |
Base configuration for a range of devices
Connected Limited Device Configuration (CLDC)
Connected Device Configuration (CDC)
Additions to configurations for a particular device
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 5 |
Basic cell phone & PDA
For CLDC libraries and virtual machine
For Java platform
High-end PDAs, set-top boxes
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 6 |
For smart cards
Call centers
Applications & services on embedded devices 1MB persistent storage + run-time cache
Obsolete Replaced by CLDC & CDC
Digital television receivers
Direct telephony control Datagram messaging Address book and calendar information User profile access Power monitoring Application installation
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 7 |
http://java.sun.com/products/midp4palm/
Based on MIDP 1.0
Platform-independent access to wireless communication resources like Short Message Service (SMS)
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 8 |
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 9 |
Scalable, small-footprint, interactive 3D API for use on mobile devices
Web services for J2ME
By Motorola/Freescale
Status unknown
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 10 |
No float or double No method that uses float or double
J2SE security model not used
Code runs in a sandbox
Must run byte-code verifier by hand before running application
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 11 |
ByteArrayInputStream |
ByteArrayOutputStream |
DataInputStream |
DataOutputStream |
InputStream |
InputStreamReader |
OutputStream |
OutputStreamWriter |
PrintStream |
Reader |
Writer |
|
Boolean |
Byte |
Character |
Class |
Integer |
Long |
Math |
Object |
Runtime |
Short |
String |
StringBuffer |
System |
Thread |
Throwable |
Calendar |
Date |
Hashtable |
Random |
Stack |
Timer |
TimerTask |
TimeZone |
Vector |
Some classes are based on JDK 1.1.8 or earlier
Some classes are smaller than their J2SE equivalents
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 12 |
import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class MySample extends MIDlet { public MySample() { } public void startApp() { Form form = new Form( "First Program" ); form.append( "Hello World" ); Display.getDisplay(this).setCurrent( form ); } public void pauseApp() { } public void destroyApp( boolean unconditional ) { } }
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 13 |
You must download and install the J2ME Wireless Toolkit 2.1
Download at:
Example will use Unix path conventions
Assume that the Wireless Toolkit (WTK) is installed in
/pathtoWTK/WTK2.1
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 14 |
Create a directory, MySample, for your project
Create subdirectories:
Place the source in file MySample.java and place file in directory src
In the directory MySample compile using:
javac -d tmpclasses -bootclasspath /pathtoWTK/WTK2.1/lib/cldcapi10.jar:/pathtoWTK/WTK2.1/lib/midpapi20.jar -classpath tmpclassese:classes src/*javapreverify -classpath
Then run
/pathtoWTK/WTK2.1/bin/preverify -classpath /pathtoWTK/WTK2.1/lib/cldcapi10.jar:/pathtoWTK/WTK2.1/lib/midpapi20.jar -d classes tmpclasses
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 15 |
Create the file
MIDlet-1: MySample, MySample.png, MySample MIDlet-Name: MySample MIDlet-Vendor: Unknown MIDlet-Version: 1.0 MicroEdition-Configuration: CLDC-1.0 MicroEdition-Profile: MIDP-2.0
Create the jar file using
jar cfm MySample.jar MANIFEST.MF -C classes .
Create the file
MIDlet-1: MySample, MySample.png, MySample MIDlet-Jar-Size: 1433 MIDlet-Jar-URL: MySample.jar MIDlet-Name: MySample MIDlet-Vendor: Roger Whitney MIDlet-Version: 1.0 MicroEdition-Configuration: CLDC-1.0 MicroEdition-Profile: MIDP-2.0
Run the emulator
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 16 |
Run the program
/pathtoWTK/WTK2.1/bin/ktoolbar
Click on New Project.
Enter the Project name and class name as shown below
Click on Create Project. A setting window will open up. Accept the defaults by clicking ok in that window.
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 17 |
Place the source code for the class in the file
WTK2.1/apps/FirstExample/src/MySample.java
In the ktoolbar main window click on the “Build” button. When the build compiles successfully then click on the “Run” button.
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 18 |
The emulator then opens up and one gets a window showing a cell phone
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 19 |
When loaded MIDlet
Starts in the Paused state
Normal instance initialization is done
If constructor throws an exception the MIDlet is destroyed
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 20 |
protected abstract void startApp() throws MIDletStateChangeException
Called when MIDlet becomes active
If a transient failures occurs:
If a non-transient failures
If any exception other than MIDletStateChangeException
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 21 |
MIDlet can have a no argument constructor
Use constructor to create resources once
Use startApp() for resources that need action each time move to active state
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 22 |
public class MySample extends MIDlet { public MySample() { } public void startApp() { Form form = new Form( "First Program" ); form.append( "Hello World" ); Display.getDisplay(this).setCurrent( form ); } public void pauseApp() {} public void destroyApp( boolean unconditional ) { } }
public class MySample extends MIDlet { public MySample() { Form form = new Form( "First Program" ); form.append( "Hello World" ); Display.getDisplay(this).setCurrent( form ); } public void startApp() {} public void pauseApp() {} public void destroyApp( boolean unconditional ) { } }
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 23 |
protected abstract void pauseApp()
Called when MIDlet is moved to Paused state from Active state
If a runtime exception occurs in pauseApp()
protected abstract void destroyApp(boolean unconditional) throws MIDletStateChangeException
Called when MIDlet is to enter the Destroyed state
Should
If unconditional is false MIDlet can
throw MIDletStateChangeException to signal not to destroy it
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 24 |
public final void notifyDestroyed()
MIDlet uses to notify it has entered the Destroyed state
destroyApp() will not be called
MIDlet must perform same operations as done by destroyApp()
public final void notifyPaused()
MIDlet uses to notify it has entered the Paused state
Application has to call resumeRequest() to reenter the Active state
public final void resumeRequest()
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 25 |
public final String getAppProperty(String key)
Returns the value of the property key
public final int checkPermission(String permission)
Added MIDP 2.0
Returns the value of the given permission
0 permission denied
1 permission granted
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 26 |
public final boolean platformRequest(String URL) throws ConnectionNotFoundException
Added MIDP 2.0
Requests that device handle the indicated URL
URL can be a
CS 683 Fall 04 | Doc 18, J2ME Intro Slide # 27 |
import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class MySample extends MIDlet { public MySample() { Form form = new Form( "First Program" ); form.append( "Hello World" ); Display.getDisplay(this).setCurrent( form ); } public void startApp() { System.out.println( “Start”); } public void pauseApp() { System.out.println( “Pause”); } public void destroyApp( boolean unconditional ) { System.out.println( “Good bye”); } }
System.out can be used for debugging. When run in the simulator, the output is put in the console, not the phone
Copyright ©, All rights reserved.
2004 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.