Lab 2: J2ME: Java 2 Micro Edition (Writing Programs for Mobile Phones using Java) Luis F. G. Sarmenta draft 3/13/2008 MIT D-Lab ICT4D.

Slides:



Advertisements
Similar presentations
MIDP Mobile Information Device Profile Johnny Yau CIS 642 Prof. Insup Lee.
Advertisements

J2ME Screen Hierarchy Displayable ScreenCanvas FormAlertListTextbox.
J2ME 25 July Overview  What is J2ME?  The CLDC and CDC configurations  MIDP and MIDlets  Development Tools  Demonstrations.
Writing Your First MIDlet Running JAVA on a Cell Phone Jon A. Batcheller January 27, 2004.
Introduction to Java 2 Micro Edition Mark Balbes, Ph.D. Senior Software Engineer Jeff Brown Senior Software Engineer.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
Developing an Application with J2ME Technology Scott Palmer, Nat Panchee, Judy Sullivan, Karen Thabet, Sten Westgard.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
PDA Programming Using J2ME Presenter :涂俊凱 Date : 2003/4/8.
Making Cell Phone Games An Overview Ray Ratelis Guild Software, Inc.
Developing J2ME Applications Mobile and Wireless Networks.
Cosc 4730 Phone Programming in Java An Introduction.
Java Platform, Micro Edition (J2ME)
Java 2 Platform, Micro Edition (J2ME) By Xiaorong Wang.
Creating Web Page Forms
J2ME and WAP Technologies CSCI – Independent Study Fall 2002 Presented by: Kashif Syed.
MIT D-Lab ICT4D Lab 1: SMS Services.
1 CGS1060 Mobile UIs Copyright 2012 by Janson Industries.
Struts 2.0 an Overview ( )
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
Chapter 9 Collecting Data with Forms. A form on a web page consists of form objects such as text boxes or radio buttons into which users type information.
EclipseME Java Micro Edition The Eclipse Way Craig Setera April 19, 2006.
Java: Chapter 1 Computer Systems Computer Programming II Aug
J2ME Java for Mobile Environments Based on J2ME In A Nutshell, by Kim Topley, O’Reilly & Associates Inc., 2002, and Tetris by Alexei Patinov.
An Introduction to the Java ME Project Jens A Andersson.
PROG Mobile Java Application Development PROG Mobile Java Application Development Event Handling Creating Menus.
Lab 8 – C# Programming Adding two numbers CSCI 6303 – Principles of I.T. Dr. Abraham Fall 2012.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 12 Using the KToolbar Rob Pooley
Platforms and tools for Web Services and Mobile Applications J2ME based Applications Bent Thomsen Aalborg University 3rd and 4th of June 2004.
Advanced Forms Lesson 10.
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
Getting Started with JXTA for J2ME Advisor : Dr. Ching-Long Yeh Report : DI.2, 洪泰昌 ( ) Course: XML 文件管理.
Michael Brockway Advanced Applications Development in Java J2ME -- Introduction l Introduction / Overview l Java 2 Micro Edition n CLDC n MIDP l Examples.
Java: Chapter 1 Computer Systems Computer Programming II.
Creating a Web Site to Gather Data and Conduct Research.
Framework and application bytecode size CLDC MIDP kXML parser Utility classes Graphics Logic (MIDlet) Application Data (XML file)
Computer Science Wi-Fi App using J2ME and MVC Architecture Advisor : Dr. Chung-E Wang Department of Computer Science Sacramento State University Date:
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Tutorial 121 Creating a New Web Forms Page You will find that creating Web Forms is similar to creating traditional Windows applications in Visual Basic.
J2ME Presented by May Sayed & Menna Hawash. Outline Introduction “Java Technology” Introduction “What is J2ME?” J2ME Architecture J2ME Core Concepts 
Tutorial 7 Creating Forms. Objectives Session 7.1 – Create an HTML form – Insert fields for text – Add labels for form elements – Create radio buttons.
1Basics of J2ME. 2 Objectives –Understand the different java API’s and how the mobile edition API’s fit in –Understand what a mobile configuration and.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 12 lcdui Rob Pooley
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 13 lcdui and OXO Rob Pooley
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 14 Various MIDlet examples Rob Pooley
CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.
Chapter 8 Collecting Data with Forms. Chapter 8 Lessons Introduction 1.Plan and create a form 2.Edit and format a form 3.Work with form objects 4.Test.
 Whether using paper forms or forms on the web, forms are used for gathering information. User enter information into designated areas, or fields. Forms.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Microsoft Access 2010 Chapter 10 Administering a Database System.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
IPSOS / Vodafone / Novartis Kenya 17 December 2014.
Chapter 5 Introduction To Form Builder. Lesson A Objectives  Display Forms Builder forms in a Web browser  Use a data block form to view, insert, update,
Java 2 Platform, Micro Edition (J2ME)
Lesson 1 1 LESSON 1 l Background information l Introduction to Java Introduction and a Taste of Java.
1 Introduction to J2ME Outline MIDP Building J2ME Apps- Tool J2ME Wireless Toolkit Demo MIDlet Programming -- MIDlet Transition States -- Midlet Skeleton.
PartII. Key M&E requirements:  Specification of information requirements  What exactly do the decision makers want to know about the project?  For.
Advance Computer Programming Market for Java ME The Java ME Platform – Java 2 Micro Edition (J2ME) combines a resource- constrained JVM and a set of Java.
Java N Amanquah.
Lab 3: More Phone-Based Techniques
Chapter 1: An Introduction to Visual Basic 2015
Java programming lecture one
By Jeff Heaton An Overview of J2ME By Jeff Heaton
Chap 7. Building Java Graphical User Interfaces
J2ME Command Class.
J2ME User Interface I.
Java for Mobile Devices
Presentation transcript:

Lab 2: J2ME: Java 2 Micro Edition (Writing Programs for Mobile Phones using Java) Luis F. G. Sarmenta draft 3/13/2008 MIT D-Lab ICT4D

D-Lab ICT for Development Lab 2: SMS Services Slide 2 Recap: Mobile Phone Capabilities, Apps SMS (Text Messaging) –based services –send text commands, receive info –receive alerts and subscriptions MMS (Multimedia Messaging) –based services –send and receive multimedia to/from server J2ME (Java 2 Micro Edition) applications –programs running on the phone –e.g., games Internet/Web services –via WAP (limited) and/or GPRS (dialup-speed connection) –via 3G (broadband speed connection) Location-based services –services that make use of location of users Micropayment applications –ability to send/transfer cellphone credits via SMS –leads to cash-less, credit-card less, electronic payment mechanisms

Overview

D-Lab ICT for Development Lab 2: SMS Services Slide 4 What is J2ME for? Writing Programs that run on the phone –no need to be connected online –uses processor and interface of phone Applications –Games for fun, but can be educational –Calculation / Computation tools e.g., medical calculators, etc. –Provide a better user interface to a service e.g., Maps for mobile (Google, Microsoft, et al) can be useful for data forms

D-Lab ICT for Development Lab 2: SMS Services Slide 5 What J2ME lets you do Computation / logic –general-purpose language –limited only by processor speed and memory Graphics –including 3D on new phones Connectivity –SMS, MMS, Bluetooth, HTTP Other Features –NFC/RFID, Location, etc. “Write Once Run Anywhere” –… IF you write your program well, AND your device supports all optional features you use

D-Lab ICT for Development Lab 2: SMS Services Slide 6 Development or Free Use –via Bluetooth, cable, or infrared –“free” download over the web via GPRS/3G download the jad file first, which makes phone download the jar file Note: you may pay traffic cost Commercial deployment –packaged with phone –download from the MNO –download from a software vendor/aggregator’s web site –request by SMS –(all of these usually require network connectivity, but may not charge you traffic cost) Deployment and Business Model

J2ME Application Development

D-Lab ICT for Development Lab 2: SMS Services Slide 8 References Book (Highly Recommended!) –Li and Knudsen. Beginning J2ME: From Novice to Professional, Third Edition. Published by Apress. ( Examples –The Sun Wireless Toolkit has many examples! –See also Lectures –

D-Lab ICT for Development Lab 2: SMS Services Slide 9 Tools Java Sun Wireless Toolkit Eclipse EclipseME

D-Lab ICT for Development Lab 2: SMS Services Slide 10 Mobile Phone for Our Labs (Thanks to Nokia Research Center Cambridge!) N82 and N95 –camera, 3G, GPS, TV out E61i –QWERTY keyboard 6131 NFC –mid-range phone with NFC (contactless RFID) reader N810 –Internet tablet (not a phone) –hi-res screen, GPS, camera, QWERTY, etc –low-end phone –only $9.99 for an official refurb unit, no contract!

D-Lab ICT for Development Lab 2: SMS Services Slide 11 Why High-End Phones? You can always simulate a low-end phone with a high- end phone … but not the other way around For some projects, using high-end phones is OK, since only a few people (e.g., health workers) need to have these Also … prices will inevitably go down in the future In any case, we will encourage designing solutions that have the broadest impact –e.g., SMS-based services and platform-independent J2ME

Basics from MIT AITI lecture in Kenya by Mike Gordon (MIT) Summer 2007

D-Lab ICT for Development Lab 2: SMS Services Slide 13 Compilation for J2ME Extra steps versus desktop Java: –Compilation using Java compiler Must include the J2ME Java libraries –Pre-verification of bytecode –Package the classes application for deployment Create a jar archive of the class files All this is done for you in the Java Wireless Toolkit

D-Lab ICT for Development Lab 2: SMS Services Slide 14 Terminology Soft Buttons Select (OK) Button Navigation (Arrow) Buttons

D-Lab ICT for Development Lab 2: SMS Services Slide 15 CLDC/MIDP Applications All cell phone applications inherit from the MIDlet class –javax.microedition.midlet.MIDlet The MIDlet class defines 3 abstract methods that the cell phone app must override: –protected abstract void startApp(); –protected abstract void pauseApp(); –protected abstract void destroyApp(boolean unconditional);

D-Lab ICT for Development Lab 2: SMS Services Slide 16 MIDlets These methods are called by the J2ME runtime system (interpreter) on your phone. –startApp(), when application is started –pauseApp(), when application is paused –destroyApp(boolean) when application is exited

D-Lab ICT for Development Lab 2: SMS Services Slide 17 Life Cycle of a MIDlet Paused ActiveDestroyed

D-Lab ICT for Development Lab 2: SMS Services Slide 18 Differences Between J2SE and CLDC/MIDP No floating point (before CLDC 1.1) System.out.print/println don’t do anything on real phone Subset of java.lang –Limited implementation of many classes Very limited java.util / java.io Make sure you are reading the JavaDoc for the J2ME MIDP when you are developing!

D-Lab ICT for Development Lab 2: SMS Services Slide 19 The MIDlet Philosophy Abstraction: –Specify the user interface in abstract terms –Just specify the components to add –A limited set of predefined components –Let the MIDP implementation decide on the placement and appearance Example –add a “done” command somewhere on the screen

D-Lab ICT for Development Lab 2: SMS Services Slide 20 The MIDlet Philosophy The device’s display is represented by an object of the Display class –Think of it as an easel Objects that can be added to a Display are subclasses of Displayable –Canvas on the easel MIDlets change the display by calling setCurrent(Displayable) in Display

D-Lab ICT for Development Lab 2: SMS Services Slide 21 The MIDlet Philosophy 1.Show a Displayable with something on it 2.Wait for input from user 3.Decide what Displayable to show next and what should be on this Displayable. 4.Go to 1.

D-Lab ICT for Development Lab 2: SMS Services Slide 22 Example Application: ToDoList

D-Lab ICT for Development Lab 2: SMS Services Slide 23 The Displayable Hierarchy Displayable Screen Alert List Form TextBox Canvas The Screen sub-classes are abstract, meaning it is up the MIDP implementation to decided on their appearance All these classes are defined in javax.microedition.lcdui

D-Lab ICT for Development Lab 2: SMS Services Slide 24 Commands A command is something the user can invoke We don’t really care how it is shown on the screen Example: –Command c = new Command(“OK”, Command.OK, 0); You can add commands to a Displayable using: –public void addCommand(Command) Commands

D-Lab ICT for Development Lab 2: SMS Services Slide 25 Responding to Command Events When a Command is invoked by the user, a method is called to service the command The exact method is: public void commandAction( Command c, Displayable d) c is the Command invoked and d is the Displayable the Command was added to.

D-Lab ICT for Development Lab 2: SMS Services Slide 26 Responding to Command Events We need to tell the Displayable the object in which to call commandAction() Two Steps: 1.The class of the object must implement the interface CommandListener CommandListener defines commandAction() 2.You tell the Displayable which object by calling setCommandListener(CommandListener) on the Displayable

D-Lab ICT for Development Lab 2: SMS Services Slide 27 Example import javax.microedition.lcdui.*; import javax.microedition.midlet.MIDlet; public class HelloWorld extends MIDlet implements CommandListener { private static Command CMD_EXIT = new Command("Exit", Command.EXIT, 0); private static Command CMD_NEXT = new Command("Next", Command.OK, 0); private TextBox textBox1; private TextBox textBox2;

D-Lab ICT for Development Lab 2: SMS Services Slide 28 Example public void startApp() { textBox1 = new TextBox("TextBox1", "The first Displayable", 30, TextField.ANY); textBox1.addCommand(CMD_NEXT); textBox1.setCommandListener(this); textBox2 = new TextBox("TextBox2", "The second Displayable", 30, TextField.ANY); textBox2.addCommand(CMD_EXIT); textBox2.setCommandListener(this); Display.getDisplay(this).setCurrent(textBox1); }

D-Lab ICT for Development Lab 2: SMS Services Slide 29 Example public void commandAction(Command c, Displayable d) { if (d == textBox1 && c == CMD_NEXT) Display.getDisplay(this).setCurrent(textBox2); else if (d == textBox2 && c == CMD_EXIT) notifyDestroyed(); } public void pauseApp() { } public void destroyApp(boolean u) { }

D-Lab ICT for Development Lab 2: SMS Services Slide 30 Example Run

Forms A form includes collection of UI controls called Items public Form(String title) public Form(String title,Item[] items) public int append() public void set(int index,Item item) public void delete(int index) public void deleteAll() public int size() public Item get(int index)

Forms example Form form = new Form(“Form Title”); StringItem strItem = new StringItem(“Label:”, “Value”); form.append(strItem);

Forms - Items String, textfield, image Items, datefield Choice Group – similar to Lists before events and item changes as well Can create custom items to use on your own and now you can build up almost any type of UI component to make your needs

Example

D-Lab ICT for Development Lab 2: SMS Services Slide 35 Example: Form and SMS import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class Hello extends MIDlet implements CommandListener { private static Command CMD_EXIT = new Command("Exit", Command.EXIT, 0); private static Command CMD_OK = new Command("OK", Command.OK, 0); private Form form; private TextField textField; public Hello() { } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } protected void pauseApp() { } protected void startApp() throws MIDletStateChangeException { form = new Form(""); textField = new TextField("Name", "", 20, TextField.ANY); form.append(textField); form.addCommand(CMD_EXIT); form.addCommand(CMD_OK); form.setCommandListener(this); Display.getDisplay(this).setCurrent(form); } public void okPressed() { SMSLibrary.sendShortMessage("Hello " + textField.getString(), " "); } public void commandAction(Command c, Displayable d) { if (c == CMD_OK) { okPressed(); } else if (c == CMD_EXIT) { notifyDestroyed(); }

D-Lab ICT for Development Lab 2: SMS Services Slide 36 Some Notes Don’t forget to add the Midlet class file to the list of Midlets in the jad Make sure Java Compiler is set to generate 1.1 code

J2ME Basic GUI Programming from MIT AITI lecture in Kenya by Mike Gordon (MIT) Summer 2007

Graphics

Other Features

D-Lab ICT for Development Lab 2: SMS Services Slide 40 SMS and Messaging API For simple sending, just use the SMSLibrary class provided for you See Knudsen book for more details and more advanced features –receiving SMS –sending and receiving MMS –binary SMS –etc.

D-Lab ICT for Development Lab 2: SMS Services Slide 41 NFC (Contactless) NFC – Near Field Communications –communicate with contactless / “RFID” devices Supported by Nokia 6131 NFC phone and a few others Reading Unique ID is simple You can also send more complex commands to the NFC Download Nokia 6131 NFC from forum.nokia.com Get NFC tags from Rich Fletcher

D-Lab ICT for Development Lab 2: SMS Services Slide 42 Other Features GUI Controls and Graphics Location Security Bluetooth Web etc. See Knudsen book for Bluetooth and Web access See Sun Wireless Toolkit Examples for other examples

D-Lab ICT for Development Lab 2: SMS Services Slide 43 Exercise Collect user’s input and send it via SMS to server –no need to receive the SMS via J2ME for now

J2ME Basic GUI Programming from MIT AITI lecture in Kenya by Mike Gordon (MIT) Summer 2007

J2ME GUI Idea: Use abstractions to support many different mobile devices, different screen sizes, colors, different input types Displayables –Alerts –Lists –Form –TextBox

Textbox allows the user to enter a String (zipcode,name,password) depending on input may be a tedious process public TextBox(String title, String text, int maxSize, int constraints) – title = screen title – text = initial text on screen – maxSize = maximum size of text box – constraints – restrict input

TextBox - Constraints Constrain the input characters –TextField.ANY – allows any type of input supported by the device –TextField.NUMERIC– restricts to only integers –TextField.DECIMAL– allows numbers with fractional parts –TextField.PHONENUMBER – requires a telephone number –TextField. ADDR – requires an address –TextField.URL – requires a web address

Text Box - Flags Flags define behaviour, opposed to restricting it –TextField.PASSWORD –TextField.UNEDITABLE –TextField.SENSITIVE –TextField.NON_PREDICTIVE –TextField.INITIAL_CAPS_WORD –TextField.INTIAL_CAPS_SENTENCE No Validation than use TextField.ANY and 0 for constraints parameter

Text Box - Flags Combine flags and constraints with | (or) Displayable d = new TextBox(“ ”,64,TextField.ANY | TextField.PASSWORD);

Alerts timed – certain amount of time – “Your transaction complete” modal – until user dismisses it – “are you sure you want to quit?” “exit without saving?” – alert types: ALARM, CONFIRMATION,ERROR,INF O, and WARNING

Alerts public Alert() or public Alert(String title, String alertText, Image alertImg, AlertType alertType) any or all parameters can be null – default timeout, but can change timeout length – Forever timeout means that it is modal

Alerts You can create an alert with: Alert alt = new Alert(“Sorry”, “I Am sorry Dave”, null, null); Set the timeout to 5 seconds by: alt.setTimeout(5000); Make it a modal alert by: alt.setTimeout(FOREVER);

Lists users select items (called elements) from choices Exclusive -Single Selection – ex. radio buttons Multiple – Multiple Selection – ex. check list Text String or image is used to represent each element

Lists Exclusive Multiple

Creating Lists public List(String title, int type) public List(String title, int type,String[] stringElements, Image[] imageElements)

Modifying Lists public void set(int elementNum, String stringPart, Image imagePart) public void insert(int elementNum, String stringPart, Image imagePart) public int append(String stringPart, Image imagePart)

Modifying Lists public String getString(int elementNum) public String getImage(int elementNum) public void delete(int elementNum) public void deleteAll() public boolean isSelected(int index) public int getSelectedIndex() public void setSelectedIndex(int index, boolean selected)

Forms A form includes collection of UI controls called Items public Form(String title) public Form(String title,Item[] items) public int append() public void set(int index,Item item) public void delete(int index) public void deleteAll() public int size() public Item get(int index)

Forms example Form form = new Form(“Form Title”); StringItem strItem = new StringItem(“Label:”, “Value”); form.append(strItem);

Forms - Items String, textfield, image Items, datefield Choice Group – similar to Lists before events and item changes as well Can create custom items to use on your own and now you can build up almost any type of UI component to make your needs

Form Layout Left to right, rows top to bottom items have labels, and can also have commands set size set layouts for individual items –setLayout(); getLayout(); –LAYOUT_LEFT, LAYOUT_CENTER....

Commands Command c = new Command(“OK”, Command.OK, 0); To Create a command, you need a name, type and also a priority. Ex: public void addCommand(Command cmd) public void removeCommand(Command cmd)

Command Types There are different types of commands available for you to use: Command.OK – Confirms a selction Command.CANCEL – Cancels pending changes Command.BACK – Moves the user back to a previous screen Command.STOP – Stop a running operation Command.HELP – Shows application Instructions Command.SCREEN – indicates generic type for specific application commands Command c = new Command("Launch", Command.SCREEN, 0);

J2ME GUI Resources Knudsen and Li. Beginning J2ME: From Novice to Professional, Third Edition. Apress. ( Sun's J2ME GUI tutorialhttp://developers.sun.com/mobility/midp/articles/ui/ –A powerful dual-license open-source GUI Designer which may be useful for your own projects beyond this course