IT 601: Mobile Computing
Download
Report
Transcript IT 601: Mobile Computing
Mobile Computing
Session 10
J2ME
S. K. Nayak
Synergy, Dhenkanal
Session: 10
S. K. Nayak
0.1
Why Java™ Technology
for Small Devices?
• The nature of wireless devices is
changing
• Old devices:
– All the software hard-coded by the
device manufacturer
• New devices:
– Can be customized by dynamically
loading software over the air
– New opportunities for
manufacturers, operators, and
developers!
Session: 10
S. K. Nayak
10.2
JAVA Architecture
Session: 10
S. K. Nayak
10.3
Overview of the Java 2 Platform
• J2EE
– A comprehensive platform for multi-user, enterprise-wide
applications.
– based on J2SE and adds APIs for server-side computing.
• J2SE
– designed for desktop computers.
– Mostly runs on top of OS X, Linux, Solaris, or Microsoft
Windows.
• J2ME
– subset of J2SE components
– set of technologies and specifications developed for small
devices like pagers, mobile phones, and set-top boxes.
– J2ME uses smaller virtual machines and leaner APIs.
Session: 10
S. K. Nayak
10.4
Elements of J2ME Architecture
• Profile: Defines the Environment
– API exposing the functionality of a specific class of target
devices, and necessary to support a particular set of services
• Optional Package: Augments the Environment
– APIs exposing specific functionality
– Deployment determined by the Platform Vendor
• Configuration: Defines the VM
– API exposing the minimal sized, preexisting
– profile defined for the specific VM it is deployed against.
– VM is either a full JVM or some subset of it (KiloByte VM).
Session: 10
S. K. Nayak
10.5
Elements of J2ME Architecture
Session: 10
S. K. Nayak
10.6
Configurations
• Connected, Limited Device Configuration(JSR-30,139)
– Smallest mobile devices
• Phones, Pagers, PDAs (small)
– 128K to 2MB with Profile and Optional Packages
– Network connectivity usually limited
• Connected Device Configuration (JSR-36)
– Larger devices, some mobile, some not
• TVs, PDAs (larger), Communicators, Cars,
Gateways
– 2MB and up with Profile and Optional Packages
– Connectivity includes TCP/IP
Session: 10
S. K. Nayak
10.7
J2ME Connected Limited
Device Configuration (CLDC)
Session: 10
S. K. Nayak
10.8
Profiles for CLDC
• Mobile Information Device Profile (JSR-37, 118)
– Will work with CLDC
– Volume Wireless Handsets
– LCDUI based GUI
• PDA Profile (JSR-75)
– References MIDP
– Smaller memory PDAs
– AWT based GUI
Session: 10
S. K. Nayak
10.9
J2ME Mobile Information
Device Profile (MIDP)
Session: 10
S. K. Nayak
10.10
J2ME-CLDC Stack
Session: 10
S. K. Nayak
10.11
MIDlet Life Cycle
• MIDP applications, or “MIDlets”, move
from state to state in their lifecycle
according to a state diagram
• MIDlet states include:
– Paused—initialized and quiescent
(waiting)
– Active—has resources and is executing
– Destroyed—has released all resources,
destroyed threads, and ended all activity
Session: 10
S. K. Nayak
10.12
J2ME APIs
J2SE APIs require several megabytes of memory
– not suitable for small devices with limited resources.
• APIs for the CLDC, provide a minimum set of libraries useful for
application development and profile definition for a variety of small
devices.
• The CLDC library APIs can be divided into the following two
categories:
•
– Classes that are a subset of the J2SE APIs:
java.lang, java.io, and java.util packages
– Classes specific to the CLDC:
These classes are located in the javax.microedition package
Session: 10
S. K. Nayak
10.13
CLDC classes inherited from J2SE
Package
Classes
java.lang
Boolean, Byte, Character, Class, Integer, Long, Math,
Object, Runnable, Runtime, Short, String,
StringBuffer, System, Thread, Throwable
java.io
ByteArrayInputStream, ByteArrayOutputStream,
DataInput, DataOutput, DataInputStream,
DataOutputStream, InputStream, OutputStream,
InputStreamReader, OutputStreamWriter,
PrintStream, Reader, Writer
java.util
Calendar, Date, Enumeration, Hashtable, Random,
Stack, TimeZone, Vector
Session: 10
S. K. Nayak
10.14
CLDC Specific Classes
Packages
javax.microedition.io
Session: 10
Classes
Connection,
ConnectionNotFoundException,
Connector, ContentConnector,
Datagram,
DatagramConnection,
InputConnection,
OutputConnection,
StreamConnection,
StreamConnectionNotifier
S. K. Nayak
10.15
MIDP Specific Classes
• MIDP-specific classes include
– the javax.microedition.rms,
– javax.microedition.midlet,
– javax.microedition.lcdui packages
• Some additional classes, interfaces, and exception classes
available:
– IllegalStateException class in the java.lang package
– Timer and TimerTask classes in the java.util package
– HttpConnection interface for HTTP protocol access over the
network in the javax.microedition.io package
Session: 10
S. K. Nayak
10.16
J2ME vs J2SE
• Reduced number support
– float, double primitives
– Double, Float classes
• Extendable UI components
– Final High-level components
– New components subclass Canvas
• Lacks:
– JNI
– Sound API
– Serialization
– Reflection
Session:
S. K. Nayak
–10User defined class loaders
10.17
Some Links to Start with J2ME
• http://developers.sun.com/techtopics/mobility/learn/
midp/getstart
• http://developers.sun.com/techtopics/mobility/midp/
articles/wtoolkit/
• http://developers.sun.com/techtopics/mobility/midp/
articles/tutorial2/
• http://developers.sun.com/techtopics/mobility/midp/
articles/api/
Session: 10
S. K. Nayak
10.18
Sun J2ME Wireless Toolkit
• Install J2SE SDK
– Available at http://java.sun.com/j2se/.
– Add bin subdirectory to the path.
• Install J2ME Wireless Toolkit
– Available at http://java.sun.com/products/j2mewtoolkit/ .
– Run KToolbar.
• Use Any IDE for coding java
– Eclipse, netbeans etc.
Session: 10
S. K. Nayak
10.19
MIDP Development with J2MEWTK
1.
2.
3.
4.
5.
Write your Java™ application (midlets) using any text
editor or IDE
Use J2ME Wireless Toolkit, Create a Project
Follow the instructions and copy the source file to the
location specified in the Wireless Toolkit
Click on Build
Run against any emulator
Session: 10
S. K. Nayak
10.20
Sun J2ME Wireless Toolkit
Session: 10
S. K. Nayak
10.21
Creating a new Project
Session: 10
S. K. Nayak
10.22
Session: 10
S. K. Nayak
10.23
Session: 10
S. K. Nayak
10.24
Session: 10
S. K. Nayak
10.25
Server Side Implementation
MIDlet: The application, a MIDlet, was designed for the Mobile
Information Device Profile, one of the J2ME specifications.
●
Servlet: Servlets are Java programs which run on the server.
●
The MIDlets connect to servlets, which implement server side
functionality.
●
Next slides show how to write and deploy a servlet, and then how to
hook up a MIDlet with the servlet
●
Session: 10
S. K. Nayak
0.26
Installing Tomcat
• Download Tomcat server
– Available at http://jakarta.apache.org/builds/jakarta-tomcat4.0/nightly
– Unpack the distribution in to a directory and set the
environment variable CATALINA_HOME to the path of the
directory
• Upon starting the server, Tomcat 4.0 will be available by
browsing: http://localhost:8080/
• Further details of Installation refer to
http://jakarta.apache.org/tomcat/tomcat-4.1-doc/RUNNING.txt
Session: 10
S. K. Nayak
10.27
Writing a Simple Servlet
HitServlet simply counts the number of times it's been invoked and writes back to the client a message
containing the count.
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class HitServlet extends HttpServlet {
private int mCount;
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String message = "Hits: " + ++mCount;
response.setContentType("text/plain");
response.setContentLength(message.length());
PrintWriter out = response.getWriter();
out.println(message);
}
}
Save the source code in a file under the Tomcat root directory named webapps/midp/WEB-INF/classes/HitServlet.java
Session: 10
S. K. Nayak
0.28
Compiling and deploying the servlet
Compiling:
Execute the following shell commands:
#export CLASSPATH=\jakarta-tomcat-4.1.27\common\lib\servlet.jar
#javac HitServlet.java
Deploying:
you'll need to edit one of Tomcat's configuration files to tell Tomcat about the new web application. Open the conf/server.xml file with a text
editor. In this file, web applications are called contexts. Scroll down to find the Context entry for the examples web application, which
begins like this:
<!-- Tomcat Examples Context -->
<Context path="/examples" docBase="examples" debug="0"
reloadable="true" crossContext="true">
Above or below this lengthy context entry (it's closed by </Context>, many lines down), create a new context entry for your new web
application. It will look similar to the opening tag for the examples context, but you'll change the names to midp as appopriate and close the
tag inline.
<!-- MIDP Context -->
<Context path="/midp" docBase="midp" reloadable="true"/>
Once you're finished adding the context entry, save the file.
Session: 10
S. K. Nayak
0.29
Create a web-application config
Copy the following text and save it as webapps/midp/WEB-INF/web.xml under the Tomcat
root directory:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>HitServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/hits</url-pattern>
</servlet-mapping>
</web-app>
Session: 10
S. K. Nayak
0.30
Hooking Up a MIDlet to the Servlet
Create a command action method in your MIDlet:
public void commandAction(Command c, Displayable s) {
if (c == mExitCommand)
notifyDestroyed();
else if (c == mConnectCommand) {
Form waitForm = new Form("Waiting...");
mDisplay.setCurrent(waitForm);
Thread t = new Thread() {
public void run() {
connect();
}
};
t.start();
}
}
Session: 10
S. K. Nayak
0.31
Hooking Up a MIDlet to the Servlet
The Connect method
private void connect() {
HttpConnection hc = null;
InputStream in = null;
String url = getAppProperty("HitMIDlet.URL");
try {
hc = (HttpConnection)Connector.open(url);
in = hc.openInputStream();
int contentLength = (int)hc.getLength();
byte[] raw = new byte[contentLength];
int length = in.read(raw);
in.close();
hc.close();
// Show the response to the user.
String s = new String(raw, 0, length);
mMessageItem.setText(s);
}
catch (IOException ioe) {
mMessageItem.setText(ioe.toString());
}
mDisplay.setCurrent(mMainForm);
}
Source code available here for download:
http://developers.sun.com/techtopics/mobility/midp/articles/tutorial2/src/HitMIDlet.java
Session: 10
S. K. Nayak
0.32
Build and run your MIDlet
Save it as HitMIDlet.java inside the apps/HelloSuite/src directory underneath the J2ME Wireless Toolkit
root directory.
Click on Settings..., then select the MIDlets tab. Click on Add and fill in "HitMIDlet" for both the MIDlet
name and class name. You can leave Icon blank. Click on OK and you should see both HelloMIDlet
and HitMIDlet listed.
Define a system property that HitMIDlet uses as the URL for its network connection. (This property is
retrieved in the third line of the connect() method.)
In the toolkit, click on Settings..., then select the User Defined tab. Click on the Add button. Fill in the
property name as HitMIDlet.URL; the value should be the URL that invokes HitServlet, the same
URL you used in a browser to test the servlet. When you're finished, click on OK to dismiss the project
settings window.
Click on Build to build the project. Make sure your server is running first.
Then click on Run and select HitMIDlet. Select the Connect command.
Session: 10
S. K. Nayak
0.33
MIDP Programming
• General MIDP Tutorials
http://developers.sun.com/techtopics/mobility/midp/reference/techart/
• Useful Links
– Event Handling (http://developers.sun.com/techtopics/mobility/
midp/articles/event/index.html)
– Client Server Communication( http://developers.sun.com/
techtopics/mobility/midp/ttips/clientserv/index.html)
– Session Handling ( http://developers.sun.com/techtopics/
mobility/midp/articles/sessions/index.html)
Session: 10
S. K. Nayak
10.34
Recommended Programming Practices
• Keep in mind that there are strict memory constraints on
most CLDC target devices
• Allocate memory resources just before use
• Free resources as soon as they are not needed
• Avoid allocating small temporary chunks of memory to avoid
heap fragmentation
• Make local copies of global variables
• Avoid redundant method calls, and inline functions rather
than creating a new class
Session: 10
S. K. Nayak
10.35
References
• J2ME
– http://java.sun.com/j2me/
– http://www.billday.com/j2me/
• Java FAQ
– http://java.sun.com/products/jdk/faq.html
• Online Java Tutorial
– http://java.sun.com/docs/books/tutorial/
• JSP Tutorial
– http://www.jsptut.com/
Session: 10
S. K. Nayak
10.36