In this section, we cover in more detail the MIDP
APIs not related to GUI development. Focus here is given to the
persistent data, Media, Networking and Push Registry APIs.
1. Record Management System
MIDP provides a simple
record-based persistent storage mechanism known as the Record Management
System (RMS). It's a package that allows the MIDlet application to
store persistent data within a controlled environment, while maintaining
system security. It provides a simple, non-volatile data store for
MIDlets while they are not running. The classes making up the RMS are
contained in the javax.microedition.rms package.
Essentially, the RMS is a very small, basic database. It stores binary data in a Record within a RecordStore. MIDlets can add, remove and update the records in a RecordStore. The persistent data storage location is implementation-dependent and is not exposed to the MIDlet. A RecordStore
is, by default, accessible across all MIDlets within a suite, and MIDP
extends access to MIDlets with the correct access permissions from other
MIDlet suites. When the parent MIDlet suite is removed from the device,
its record stores are also removed, regardless of whether a MIDlet in
another suite is making use of them.
Here is a short example of how to use RMS for writing and reading persistent data from the device:
// saving data to RMS
public int saveToStore(byte[] data) {
int recordID = 0;
try {
RecordStore store = RecordStore.openRecordStore("ImageStore", true);
recordID = store.addRecord(data, 0, data.length);
store.closeRecordStore();
}
catch(RecordStoreException rse) {
rse.printStackTrace();
}
return recordID;
}
// reading data from RMS
public byte[] loadFromStore(String storeName, int recordID) {
byte[] data = null;
try {
RecordStore store = RecordStore.openRecordStore("ImageStore", false);
data = store.getRecord(recordID);
store.closeRecordStore();
}
catch(RecordStoreException rse) {
rse.printStackTrace();
}
return data;
}
2. Media API
MIDP includes the Media
API which provides limited, audio-only multimedia. It is a subset of the
optional and much richer JSR-135 Mobile Media API, which currently
ships on most Symbian OS phones.
The MIDP Media API
provides support for tone generation and audio playback of WAV files if
the latter is supported by the underlying hardware. Since MIDP is
targeted at the widest possible range of devices, not just feature-rich
smartphones, the aim of the Media API is to provide a lowest common
denominator of functionality suitable for the capabilities of all MIDP
devices.
3. Networking – Generic Connection Framework
CLDC has defined a
streamlined approach to networking, known as the Generic Connection
Framework (GCF). The framework seeks to provide a consistent interface
for every network connection between the MIDP classes and the underlying
network protocols. It doesn't matter what kind of connection is being
opened; the interface remains the same. For instance, if you're opening a
socket or an HTTP connection, you are still going to use the same Connector.open()
method. MIDP has support for many protocols, although HTTP and HTTPS
are mandatory. Java ME on Symbian OS also supports other optional
protocols, such as sockets, server sockets and datagrams.
4. Push Registry
The Push Registry API
allows MIDlets to be launched in response to incoming network
connections. Many applications, particularly messaging applications,
need to be continuously listening for incoming messages. To achieve this
in the past, a Java application would have had to be continually
running in the background. Although the listening Java application may
itself be small, it would still require an instance of the virtual
machine to be running, thus appropriating some of the mobile phone's
scarce resources. The JSR-118 group recognized the need for an
alternative, more resource-effective solution for MIDP and so introduced
the Push Registry.