J2ME Introduction by k9902mn

VIEWS: 199 PAGES: 20

									INTRODUCTION TO J2ME




 Presented By : Kinjal Khandhar


 Under the guidance of :
 Prof. Anita Wasilewska & Prof. Jennifer Wong
    J2ME - INTRO

   It is a platform, a collection of technologies & specifications for small devices
   Contains classes and methods to develop applications for mobile devices
   J2ME Architecture :
    Basic Components:Configurations,Profiles,Optional   packages
COMPONENTS

   Configurations :
    - Defines minimum Java tech that developer can expect on broad range of implementing
    devices
    - Provide base functionality for a range of devices sharing similar characteristics like
     network connectivity & memory footprint
    - There are two J2ME configurations:
           1. Connected Limited Device Configuration (CLDC 1.0/1.1)
               -> For phones,pager,wireless devices.
               -> Mostly Wireless
               ->160 - 512k of memory available for Java
               -> has limited power or battery operated
           2. Connected Device Configuration(CDC/CDC 1.1)
                -> digital television set-top boxes, high end wireless devices,etc
               -> powered by a 32-bit processor
                -> 2MB or more of total memory available for Java


    - CLDC 1.0 APIs : java.lang,java.io,java.util,javax.microedition.io
COMPONENTS (..CONTD)
   Profiles :
    Includes API’s for user interface and persistent storage
    - There are four J2ME profiles:
          1. Mobile Information Device Profile (MIDP 1.0 / 2.0)
              - For CLDC devices
          2. Foundation Profile
               - For CDC devices
          3. Personal Profile
              - For CDC devices
          4. Personal Basis Profile
              -For CDC devices
    - MIDP API’s : Advanced networking, security, gaming, and media
    features

   Packages : Functionalities that may not be associated with a particular
    configuration and profile
    - Eg : Bluetooth API,Wireless messaging API WMA,J2ME web services
INTRODUCTION TO MIDLET
   MIDlet – MID Profile Application :
    - J2ME optimized servlet API inherits from javax.microedition.midlet.MIDlet
    - Java application framework for the MIDP that is typically implemented on a Java-
    enabled cell-phones or other embedded devices or emulator.
    - MIDlets are applications, such as games.
   LifeCycle of a MIDlet (CallBack Methods):
    - startApp()      : Initialize or resume paused
    - pauseApp()      : Pause application
    - destroyApp() : Cleanup and release all resources
    MIDLETS


   MIDlet suite : Midlets packaged together
   A Java Archive (JAR) file
    Contains the user-defined classes, images and sounds that make up the application and JAR file
    manifest that describes the attributes of the MIDlet
   A Java Descriptor (JAD) file
     - It permits a device to examine the descriptor before downloading the whole MIDlet suite
     - Can contain the location of and describe the contents of .jar file

   Requirements to run on mobile phone :
    - The main class needs to be a subclass of javax.microedition.midlet.MIDlet
    - The MIDlet needs to be packed inside a .jar file (e.g. by using the jar-tool)
    -The .jar file needs to be pre-verified by using a preverifier.


    Unlike a Java applet, a MIDlet is limited to use of the LCDUI rather than the more
    familiar widgets of AWT and Swing.
DEPLOYING MIDLETS :


- General Application : After testing the application over emulator, download the
 jar file on mobile and test it

- Network Application : First test it on a live wireless network
- Remote Application :
    1. Upload your application files (JAD and JAR) to a remote web server
   2. Reconfigure the web server so that it recognizes JAD and JAR files.
      For the JAD file type, set the file extension to .jad and the MIME
      type to text/vnd.sun.j2me.app-descriptor.
   3. For the JAR file type, set the file extension to .jar and the MIME
      type to application/java-archive.
   4. Change your JAD file's MIDlet-Jar-URL property to specify the URL of
      your JAR file
DEMO:WRITING A SIMPLE J2ME APPLICATION
    DATABASE - RMS
   Record : An individual data item. No restrictions on what goes into the record(i.e. record can
    contain a number, a string, an array, an image -- anything that a sequence of bytes can represent)


   Record Stores : Ordered collection of records. Records are not independent entities: each must
    belong to a record store, and all record access occurs through the record store. In fact, the record
    store guarantees that records are read and written atomically, with no possibility of data
    corruption(javax.microedition.rms.RecordStore)


   Small databases that contain records that are byte arrays identified by names


   Scope of a record store:
    •Single MIDlet
    •Shared between MIDlets
    •Not accessible by native applications
    •Sensible data should be encrypted




   Storage Limits : The amount of memory available for record-based data storage
    varies from device to device. The MIDP specification requires devices to reserve at
    least 8K of non-volatile memory for persistent data storage.
CREATE,LIST,CLOSE,DELETE RECORD STORE
   Create Record Stores:
    public static RecordStore openRecordStore(String recordStoreName, boolean
    createIfNecessary) throws RecordStoreException, RecordStoreFullException,
    RecordStoreNotFoundException
    - The record store is accessible by the MIDlet that created it only
    public static RecordStore openRecordStore(String recordStoreName, boolean
    createIfNecessary, int authmode, boolean writable) throws RecordStoreException,
    RecordStoreFullException, RecordStoreNotFoundException
    - Authmode are AUTHMODE_PRIVATE and AUTHMODE_ANY and they define what
    MIDlets can access the record store

   List all Record Stores :
    public static String[] listRecordStores()
    Returns null if there are no record stores associated with a MIDlet.It does not list the shared
    record stores created by another MIDlet suite. Useful when populating record stores to avoid
    adding the same records several times

   Closing and Deleting Record Stores:
    To close record store : closeRecordStore must be called as many times as openRecordStore is
    public void closeRecordStore() throws RecordStoreNotOpenException, RecordStoreException

    To delete record stores and their contained records use deleteRecordStore
    public static void deleteRecordStore(String recordStoreName) throws RecordStoreException,
    RecordStoreNotFoundException
    QUERY HANDLING USING RECORDENUMERATION
   RecordEnumeration maintains a sequence of records of a record store following a
    particular pattern to be navigated back and forth


   Record Events:
    RecordListener is an interface for receiving record changed/added/deleted events from
    a record store
    Interface Methods:
    public void recordAdded(RecordStore recordStore, int recordId)
    public void recordChanged(RecordStore recordStore, int recordId)
    public void recordDeleted(RecordStore recordStore, int recordId)
WIRELESS MESSAGING API(WMA)
   API on the top of CLDC (1.0 or 1.1) and Connected Device Configuration(CDC)
   Enables MIDP applications to use the Short Message Service (SMS) and Cell
    Broadcast Service (CBS) protocols
   SMS messages are sent through a store-and-forward network
   SMPP
   Limit: 160 characters
   Applications: Chats, interactive games, event reminders, queries of corporate data
   3-tier Wireless Messaging System Architecture
    MESSAGE STRUCTURE



   sms://+phone number : Client connection that sends messages to a phone
    number
   sms://:port number : Server connection that receives messages on a port .
    It can also send messages.
   Opening a MessageConnection: MessageConnectionsare used to send and
    receive messages, but also to create messages and obtain segmentation
    information on messages
Example:
   Client connection
    MessageConnection clientConn = (MessageConnection) Connector. Open(“sms://5550000”);

   Server connection
    MessageConnection serverConn = (MessageConnection)Connector. Open("sms://:1234");
    SENDING & RECEIVING SMS MESSAGE
   Use of the send method of MessageConnection
    public void send(Message msg) throws java.io.IOException, java.io.InterruptedIOException
   E.g. public void sendText(String address, String text) {
    MessageConnection mc;
    try {
        mc = (MessageConnection) Connector. Open("sms://" + address);
        TextMessage tm = (TextMessage) mc.newMessage(MessageConnection.TEXT_MESSAGE);
        tm.setPayloadText(text);
       mc.send(tm);
    } catch (IOException e) {
        e.printStackTrace();}}


   Use the receive method of MessageConnection
    public Message receive() throws java.io.IOException, java.io.InterruptedIOException
   If there are no messages waiting, this method will block until either a message is
    received or the MessageConnection is closed
   Messages can be received using:
    - a blocking receive method : Receive is called in its own thread
    - a non-blocking receive method : Receive is managed using a MessageListener
    associated with the MessageConnection and should be called in a separate thread
 EXAMPLE
try {
msg = mc.receive();
} catch (Exception e) {…}
// Process the received message
if (msg instanceof TextMessage) { // Text message
     TextMessage tmsg = (TextMessage) msg;
     msgReceived = tmsg.getPayloadText();
} else {
     if (msg instanceof BinaryMessage) { // Binary message
     BinaryMessage bmsg = (BinaryMessage) msg;
     Byte[] data = bmsg.getPayloadData();
     msgReceived = data.toString();
} else {
     System.out.println("Other message");
     msgReceived = “Other";
}}
MULTI-THREADING
   Allows a program to run concurrently
   Concurrent programming in Java is done with threads that exist within a process
   Thread.start is used to start the new thread
   Thread.sleep causes the current thread to suspend execution for a specified period
   The join method allows one thread to wait for the termination of another
   E.g Using an anonymous inner class:
    new Thread(new Runnable() {
    public void run() {
    …
    }}).start();
STEPS TO START WITH J2ME PROGRAMMING(ECLIPSE IDE)
   Visit http://eclipseme.org/ to add the Eclipse ME plugin to the Eclipse IDE.

   Download Sun Java Wireless Toolkit 2.5.2_01 for CLDC from
    http://java.sun.com/products/sjwtoolkit/download.html?feed=JSC

   After download Run WTK to test whether it has been installed properly.

   Goto Eclipse->Window->Preferences -> J2ME ->Device Management -> Import -> Give the
    path of the WTK directory.This will search the devices available.

   Select the DefaultPhone and Save Preferences.

   Environment is ready to use!!!

   Create a New Project -> J2ME Midlet -> Select Profile and Configuration and say
    Finish.Start Coding

   Always Build Application before Run.

   All the resources needs to be packaged in same folder,so while deployment there wont be
    any issues.
STEPS TO START WITH J2ME PROGRAMMING(NETBEANS IDE)



 Download NetBeans IDE with Mobility Pack from
  http://www.netbeans.org/downloads/index.html
 No WTK is needed for running via Netbeans.It has its
  own device profile.
 Adding a plugin to NetBeans can be referred from
  http://www.netbeans.org/community/releases/36/J2ME_S
  upport.html
 PS. If possible don’t install plugin,just add the mobility
  pack to NetBeans.
    REFERENCES :

   http://www.sun.com/aboutsun/media/presskits/ctia2004/J2ME.pdf
   http://en.wikipedia.org/wiki/MIDlet
   http://www.uberthings.com/mobile/
   Professor Chistelle Sharff Lectures on Mobile Phone Programming
   http://developers.sun.com/mobility/midp/articles/databaserms/
   http://www.ibm.com/developerworks/library/wi-rms/
   http://cs.sunysb.edu/~cse651/
   http://www.digilife.be/quickreferences/pt/j2me%20step%20by%20step.pdf
   http://www.pdftutorials.com/tutorials/J2ME/J2ME-books-shelf1.html
   http://www.j2meforums.com/wiki/index.php/Tutorials
QUESTIONS ??

								
To top