Programming Mobile Devices With J2ME: Claude Fuhrer
Programming Mobile Devices With J2ME: Claude Fuhrer
October 2010
Programming mobile devices with J2ME Programming mobile devices with J2ME
1 Introduction - Introduction 2
Introduction The Connected Limited Device Configuration Introduction The Connected Limited Device Configuration
Profiles
Programming mobile devices with J2ME Programming mobile devices with J2ME
Introduction - The Connected Limited Device Configuration 3 Introduction - The Connected Limited Device Configuration 4
Introduction The Connected Limited Device Configuration Introduction The Connected Limited Device Configuration
Programming mobile devices with J2ME Programming mobile devices with J2ME
Introduction - The Connected Limited Device Configuration 5 Introduction - The Connected Limited Device Configuration 6
Introduction The Connected Limited Device Configuration Introduction The Connected Limited Device Configuration
Programming mobile devices with J2ME Programming mobile devices with J2ME
Introduction - The Connected Limited Device Configuration 7 Introduction - The Connected Limited Device Configuration 8
Introduction The Connected Limited Device Configuration Introduction The Connected Limited Device Configuration
Programming mobile devices with J2ME Programming mobile devices with J2ME
Introduction - The Connected Limited Device Configuration 9 Introduction - The Connected Limited Device Configuration 10
Introduction The Connected Limited Device Configuration Introduction The Connected Limited Device Configuration
Differences between J2ME and J2SE The KVM (Kilo Virtual Machine)
Programming mobile devices with J2ME Programming mobile devices with J2ME
Introduction - The Connected Limited Device Configuration 11 Introduction - The Connected Limited Device Configuration 12
Introduction The Connected Limited Device Configuration Introduction The Connected Limited Device Configuration
Programming mobile devices with J2ME Programming mobile devices with J2ME
Introduction - The Connected Limited Device Configuration 13 Introduction - The Connected Limited Device Configuration 14
Introduction The Connected Limited Device Configuration Introduction The Connected Limited Device Configuration
Programming mobile devices with J2ME Programming mobile devices with J2ME
Introduction - The Connected Limited Device Configuration 15 Introduction - The Connected Limited Device Configuration 16
Introduction The Connected Limited Device Configuration Introduction The Connected Limited Device Configuration
Programming mobile devices with J2ME Programming mobile devices with J2ME
Introduction - The Connected Limited Device Configuration 17 Introduction - The Connected Limited Device Configuration 18
Introduction The Connected Limited Device Configuration Introduction The Connected Limited Device Configuration
import javax.microedition.midlet.*;
The application HelloMidlet looks like: import javax.microedition.lcdui.*;
public HelloMidlet() {}
Programming mobile devices with J2ME Programming mobile devices with J2ME
Introduction - The Connected Limited Device Configuration 21 Introduction - The Connected Limited Device Configuration 22
Introduction The Connected Limited Device Configuration Introduction The Connected Limited Device Configuration
Programming mobile devices with J2ME Programming mobile devices with J2ME
Introduction - The Connected Limited Device Configuration 23 Introduction - The Connected Limited Device Configuration 24
Introduction The Connected Limited Device Configuration Introduction The Connected Limited Device Configuration
Programming mobile devices with J2ME Programming mobile devices with J2ME
Introduction - The Connected Limited Device Configuration 25 Introduction - The Connected Limited Device Configuration 26
Introduction The Connected Limited Device Configuration Introduction The Connected Limited Device Configuration
J2ME is targetted for devices with small memory footprint. There is three main reasons to obfuscate the J2ME code
Therefore, it is crucial to make your applications run faster and 1 Protection of intellectual property
use less memory. The following tips my help you to enhance There is a lot of java decompiler one can easily find on the
your programming skills. internet. Some of these decompiler produce a pretty clean java
1 Use local variable instead fields source code and therefore may help the "reverse engineering"
2 Minimize method calls of your application.
3 Avoid string concatenation. Use class StringBuffer if
2 Footprint reduction
needed. As already mentionned, the memory of a virtual machine for a
4 Minimize object creation J2ME application is small. The use of an obfuscator may help
5 Stack is faster than heap to drastically reduce the size.
6 Avoid synchronisation.
3 Improve runtime performance
An obfuscator not only make difficult the reverse engineering
Make classes final where possible of a java program, but, with the use of some parametrisation
Use obfuscation (switches) may optimise it to enhance it efficiency. Since the
"speed" of most micro-processors used into micro devices is
Write portable code
rather low, this advantage is to be considered.
Programming mobile devices with J2ME Programming mobile devices with J2ME
Introduction - The Connected Limited Device Configuration 27 Introduction - The Connected Limited Device Configuration 28
Introduction The Connected Limited Device Configuration Introduction The Connected Limited Device Configuration
Programming mobile devices with J2ME Programming mobile devices with J2ME
Introduction - The Connected Limited Device Configuration 29 Introduction - The Connected Limited Device Configuration 30
Introduction The Connected Limited Device Configuration Introduction The Connected Limited Device Configuration
Obfuscators references
Part II
Another web page providing a rich list of Java Obfuscators may be
found at the url:
http://www.dmoz.org/Computers/Programming/Languages/ MIDP Gui Programming
Java/Development_Tools/Obfuscators/
Programming mobile devices with J2ME Programming mobile devices with J2ME
Introduction - The Connected Limited Device Configuration 33 MIDP Gui Programming - 34
MIDP GUI programming MIDP GUI programming
Object
...
Gauge
Form Item
public startApp() {
display = Display.getDisplay(this);
ImageItem
TextBox
StringItem
}
TextField
Programming mobile devices with J2ME Programming mobile devices with J2ME
MIDP Gui Programming - MIDP GUI programming 37 MIDP Gui Programming - MIDP GUI programming 38
MIDP GUI programming MIDP GUI programming
Programming mobile devices with J2ME Programming mobile devices with J2ME
MIDP Gui Programming - MIDP GUI programming 39 MIDP Gui Programming - MIDP GUI programming 40
MIDP GUI programming MIDP GUI programming
Programming mobile devices with J2ME Programming mobile devices with J2ME
MIDP Gui Programming - MIDP GUI programming 41 MIDP Gui Programming - MIDP GUI programming 42
MIDP GUI programming MIDP GUI programming
TextField.URL
Input must be a URL
Programming mobile devices with J2ME Programming mobile devices with J2ME
MIDP Gui Programming - MIDP GUI programming 43 MIDP Gui Programming - MIDP GUI programming 44
MIDP GUI programming MIDP GUI programming
Programming mobile devices with J2ME Programming mobile devices with J2ME
MIDP Gui Programming - MIDP GUI programming 45 MIDP Gui Programming - MIDP GUI programming 46
MIDP GUI programming MIDP GUI programming
void initialize() {
The List class defines the following methods : myScreen = new List("EMAIL", List.IMPLICIT);
readCommand = new Command("read",
public List(String title, int listType)
Command.SCREEN, 1);
public List(String title, int listType,
replyCommand = new Command("reply",
String[] stringElements,
Command.SCREEN, 1);
Image[] imageElements)
deleteCommand = new Command("delete",
public int size()
Command.SCREEN, 1);
public String getString(int elementNum)
myScreen.addCommand(readCommand);
public Image getImage(int elementNum)
myScreen.addCommand(replyCommand);
public boolean isSelected(int elementNum)
myScreen.addCommand(deleteCommand);
myScreen.setCommandListener(this);
}
Programming mobile devices with J2ME Programming mobile devices with J2ME
MIDP Gui Programming - MIDP GUI programming 47 MIDP Gui Programming - MIDP GUI programming 48
MIDP GUI programming MIDP GUI programming
Programming mobile devices with J2ME Programming mobile devices with J2ME
MIDP Gui Programming - MIDP GUI programming 49 MIDP Gui Programming - MIDP GUI programming 50
MIDP GUI programming MIDP GUI programming
Programming mobile devices with J2ME Programming mobile devices with J2ME
MIDP Gui Programming - MIDP GUI programming 51 MIDP Gui Programming - MIDP GUI programming 52
MIDP GUI programming MIDP GUI programming
Programming mobile devices with J2ME Programming mobile devices with J2ME
MIDP Gui Programming - MIDP GUI programming 53 MIDP Gui Programming - MIDP GUI programming 54
MIDP GUI programming MIDP GUI programming
Programming mobile devices with J2ME Programming mobile devices with J2ME
MIDP Gui Programming - MIDP GUI programming 55 MIDP Gui Programming - MIDP GUI programming 56
MIDP GUI programming MIDP GUI programming
The Gauge class implements a bar graph display of a value The Gauge class defines the following methods :
intended for use in a form. public void setValue(int value)
public int getValue()
Gauge is optionally interactive.
public void setMaxValue(int maxValue)
The Gauge class defines the following constructor : public int getMaxValue()
public Gauge(String label, boolean interactive, public boolean isInteractive()
int maxValue, int initialValue) public void setLabel(String label)
Programming mobile devices with J2ME Programming mobile devices with J2ME
MIDP Gui Programming - MIDP GUI programming 57 MIDP Gui Programming - MIDP GUI programming 58
MIDP GUI programming MIDP GUI programming
Programming mobile devices with J2ME Programming mobile devices with J2ME
MIDP Gui Programming - MIDP GUI programming 59 MIDP Gui Programming - MIDP GUI programming 60
MIDP GUI programming MIDP GUI programming
Programming mobile devices with J2ME Programming mobile devices with J2ME
MIDP Gui Programming - MIDP GUI programming 61 MIDP Gui Programming - MIDP GUI programming 62
MIDP GUI programming MIDP GUI programming
Programming mobile devices with J2ME Programming mobile devices with J2ME
MIDP Gui Programming - MIDP GUI programming 63 MIDP Gui Programming - MIDP GUI programming 64
MIDP GUI programming MIDP GUI programming
The differents values defined are : The following code snippet gives an example how to use the
1 ImageItem.LAYOUT_DEFAULT ImageItem class :
2 ImageItem.LAYOUT_LEFT Image img = Image.createImage("/Duke.png");
3 ImageItem.LAYOUT_RIGHT ImageItem imgItem = new ImageItem("Image", img,
4 ImageItem.LAYOUT_CENTER ImageItem.CENTER_LAYOUT, "img");
5 ImageItem.LAYOUT_NEWLINE_BEFORE Form form = new Form("Duke");
6 ImageItem.LAYOUT_NEWLINE_AFTER form.append(imageItem);
Programming mobile devices with J2ME Programming mobile devices with J2ME
MIDP Gui Programming - MIDP GUI programming 65 MIDP Gui Programming - MIDP GUI programming 66
MIDP GUI programming MIDP GUI programming
Programming mobile devices with J2ME Programming mobile devices with J2ME
MIDP Gui Programming - MIDP GUI programming 67 MIDP Gui Programming - MIDP GUI programming 68
MIDP GUI programming MIDP GUI programming
The class TextField Guidelines for GUI programming for GUI devices
Programming mobile devices with J2ME Programming mobile devices with J2ME
MIDP Gui Programming - MIDP GUI programming 69 MIDP Gui Programming - MIDP GUI programming 70
MIDP GUI programming MIDP event model
MIDlets should never assume specific screen size; instead they Part III
should query the size of the display and adjust accordingly
Entering alphanumeric data through a handled device can be The MIDP Event Model
tedious. If possible provide a list of choices from which the
user can select.
Programming mobile devices with J2ME Programming mobile devices with J2ME
MIDP Gui Programming - MIDP GUI programming 71 The MIDP Event Model - 72
MIDP event model MIDP event model
Just like the design pattern with the same name, the Command
class encapsulates the semantic information of an action.
In AWT and Swing, events are generated when a user interact
with an application. The constructor of the class Command is defined as :
public Command(String label,
The same model holds true for the MIDP int commandType,
However, since there are two MIDP user interface APIs (high- int priority)
and low-level), there are two kinds of events. The commandType argument specifies the command’s intent.
The priority argument describes the importance of this
command relative to the other commands on the screen.
Programming mobile devices with J2ME Programming mobile devices with J2ME
The MIDP Event Model - MIDP event model 73 The MIDP Event Model - MIDP event model 74
MIDP event model MIDP event model
Programming mobile devices with J2ME Programming mobile devices with J2ME
The MIDP Event Model - MIDP event model 77 The MIDP Event Model - MIDP event model 78
J2ME security J2ME security
Programming mobile devices with J2ME Programming mobile devices with J2ME
J2ME Security - 79 J2ME Security - J2ME security 80
J2ME security J2ME security
Programming mobile devices with J2ME Programming mobile devices with J2ME
J2ME Security - J2ME security 81 J2ME Security - J2ME security 82
J2ME security J2ME security
Programming mobile devices with J2ME Programming mobile devices with J2ME
J2ME Security - J2ME security 83 J2ME Security - J2ME security 84
J2ME security J2ME security
To enforce these security restrictions, some Java features have In CLDC, the application programmer cannot override, modify or
been removed like: add any classes to the protected system package.
1 Java Native Interface (JNI) Therefore, in order to protect the sytem classes from downloaded
2 User-defined class loaders
MIDlets, system classes are always searched first when performing a
3 Thread groups or deamon threads
4 Support of reflection. class file lookup.
Programming mobile devices with J2ME Programming mobile devices with J2ME
J2ME Security - J2ME security 85 J2ME Security - J2ME security 86
J2ME security J2ME security
Programming mobile devices with J2ME Programming mobile devices with J2ME
J2ME Security - J2ME security 87 J2ME Security - J2ME security 88
J2ME security J2ME security
Programming mobile devices with J2ME Programming mobile devices with J2ME
J2ME Security - J2ME security 89 J2ME Security - J2ME security 90
J2ME security Database programming
Part V
Programming mobile devices with J2ME Programming mobile devices with J2ME
J2ME Security - J2ME security 91 Database and storage - 92
Database programming Database programming
Programming mobile devices with J2ME Programming mobile devices with J2ME
Database and storage - Database programming 93 Database and storage - Database programming 94
Database programming Database programming
Record stores have names that are case-sensitive and cannot The javax.microedition.rms package defines four
be more that 32 characters in length. interfaces, one class and five exception classes.
A MIDlet cannot create two record stores with the same name The list of the four interfaces is :
in the same application. RecordComparator defines a comparator to compare two
However, it can create a record store with the same name in records.
another application. RecordEnumeration represents a bidirectional record
enumerator
The MIDP RMS implementation ensures that all individual
RecordFilter defines a filter to examine a record and check
record store operations are atomic, synchronous and serialized,
if it matches, based on a criteria defined by the
so no corruption occurs with multiple access.
application.
Programming mobile devices with J2ME Programming mobile devices with J2ME
Database and storage - Database programming 95 Database and storage - Database programming 96
Database programming Database programming
Programming mobile devices with J2ME Programming mobile devices with J2ME
Database and storage - Database programming 97 Database and storage - Database programming 98
Database programming Database programming
Database programming with the RMS is relative To create or open a record store, you can use the constructor
straightforward. of the RecorsStore class. This constructor is :
public static RecordStore
A record store consist of a collection of records that is uniquely
openRecordStore(String recordStoreName,
identified by its record ID, which is an integer value. boolean createIfNecessary)
The first ID has an ID of 1 throws RecordStoreException,
RecordStoreFullException,
The theoretical limit of a record store is 2’147’486’647 records. RecordStoreNotFoundException
Each record in a record store can be of different length and Record store’s names are case-sensitive and may be up to 32
can each store data differently Unicode characters in length.
Programming mobile devices with J2ME Programming mobile devices with J2ME
Database and storage - Database programming 99 Database and storage - Database programming 100
Database programming Database programming
It the openRecordStore() method is called by a MIDlet To locate a particular record store among several on the
when the record store is already open by another MIDlet in the device, the API provides the following method :
same MIDlet suite, the method return a reference to the same public static String[] listRecordStores()
RecordStore object To delete en entire record store, you can use :
Once opened, the record store will eventually be closed. This public static void
can be done by the following method : deleteRecordStore(String recordStoreName)
public void closeRecordStore() throws RecordStoreException,
throws RecordStoreNotOpenException, RecordStoreNotFoundException
RecordStoreException The size of the currently opened record store may be found
It is important to note that the record store will not actually using :
be closed until the closeRecordStore() is called as many public int getSize()
times as openRecordStore() was called. throws RecordStoreNotOpenException
Programming mobile devices with J2ME Programming mobile devices with J2ME
Database and storage - Database programming 101 Database and storage - Database programming 102
Database programming Database programming
Programming mobile devices with J2ME Programming mobile devices with J2ME
Database and storage - Database programming 103 Database and storage - Database programming 104
Database programming Database programming
To read a record from record store, you can use one of the If, for example, we have a record represented by a single string,
following methods : the following code snippet may be used to add a record
public int getRecord(int recordId,
try {
byte[] buffer,
ByteArrayOutputStream baos =
int offset)
new ByteArrayOutputStream();
throws RecordStoreNotOpenException,
DataOutputStream dos = new DataOutputStream(baos);
InvalidRecordIDException,
dos.writeUTF (record);
RecordStoreException
byte b[] = baos.toByteArray();
recordNumber = db.addRecord (b, 0, b.length);
public byte[] getRecord(int recordId)
} catch (Exception e ) {
throws RecordStoreNotOpenException,
// Handle exception
InvalidRecordIDException,
}
RecordStoreException
Programming mobile devices with J2ME Programming mobile devices with J2ME
Database and storage - Database programming 105 Database and storage - Database programming 106
Database programming Database programming
Programming mobile devices with J2ME Programming mobile devices with J2ME
Database and storage - Database programming 107 Database and storage - Database programming 108
Database programming Database programming
Programming mobile devices with J2ME Programming mobile devices with J2ME
Database and storage - Database programming 109 Database and storage - Database programming 110
Database programming Database programming
Programming mobile devices with J2ME Programming mobile devices with J2ME
Database and storage - Database programming 111 Database and storage - Database programming 112
Database programming Connecting to the world
Part VI
The RecordEnumertor interface defines a mechanism to
enumerate over all the records of a record store. Connecting the world
Programming mobile devices with J2ME Programming mobile devices with J2ME
Database and storage - Database programming 113 Connecting the world - 114
Connecting to the world Connecting to the world
Programming mobile devices with J2ME Programming mobile devices with J2ME
Connecting the world - Connecting to the world 115 Connecting the world - Connecting to the world 116
Connecting to the world Connecting to the world
The Generic Connection Frameworks (cont’d) Making a Connection with HTTP Get
Programming mobile devices with J2ME Programming mobile devices with J2ME
Connecting the world - Connecting to the world 117 Connecting the world - Connecting to the world 118
Connecting to the world Connecting to the world
Programming mobile devices with J2ME Programming mobile devices with J2ME
Connecting the world - Connecting to the world 119 Connecting the world - Connecting to the world 120
Connecting to the world Connecting to the world
javax.microedition.io package
Connection CLDC 1.0
Generic Connection Framework
StreamConnection UDPDatagramConnection
Programming mobile devices with J2ME Programming mobile devices with J2ME
Connecting the world - Connecting to the world 121 Connecting the world - Connecting to the world 122
Connecting to the world Connecting to the world
Programming mobile devices with J2ME Programming mobile devices with J2ME
Connecting the world - Connecting to the world 123 Connecting the world - Connecting to the world 124