Concept Board: RecordStore demo MIDlet
Tutorial, May 8, 2014 - 1:36pm, 4568 views
EHS6 offers record store feature, which provides the possibility to save user data to non-volatile module's memory. A record store consists of a collection of records which will remain persistent across multiple invocations of the MIDlet. Record stores are created in platform-dependent locations, which are not exposed to the MIDlets. Record stores can also be shared between MIDlets. Every record in record store has unique numerical identifier.
Please see picture below for clear look.
RecordStore Java API
Provided Record Store Java API consists of following classes:
- RecordStore class - used for accessing the store
- RecordEnumeration interface - used for listing stored records
- RecordComparator interface - used for sorting enumerated records
- RecordFilter interface - used for filtering enumerated records
- RecordListener interface - used for monitoring record store events
First step in Record Store usage is opening the store:
RecordStore rs = RecordStore.openRecordStore("Diary", true);
Preparing record data
To add new record you must prepare byte array of data first. Strings can be easily converted to byte array using getBytes() method. Unfortunately there is no build-in feature to convert numerical types directly to byte array. You could use following conversion chain: number -> string -> bytes or implement own byte-by-byte conversion.
If the data is already prepared and temporarily passed to byteArray, you can use following pattern to store it:
int recordId = rs.addRecord(byteArray, 0, byteArray.length);
Record ID is returned value.
Follow below code pattern to enumerate records.
RecordEnumeration re = enumerateRecords(filter, comparator, keepUpdated);
// iterate over selected records
record = re.nextRecord();
// process record...
nextRecord method returns byte array for further processing.
Make sure that filter and comparator objects implement RecordFilter and RecordComparator interfaces. Pass null if no filter or comparator is needed.
To delete a record simply call:
Example diary application concept
As an example we will implement an application which will fulfill following requirements:
- Application listens for buttons events: BTN-A pressed, BTN-A released, BTN-B pressed, BTN-B released.
- All events occurances are saved to record store as separate records with timestamp and event type (pressed/released).
- MIDlet is terminated by releasing BTN-B.
- All records are printed on stdout on application start and shutdown.
Please find eclipse project in tutorial's attachment. Follow in-file documentation for details.
Thanks to Record Store you can save non-volatile user data like log information, application state or configuration settings with minimal developing effort without worrying about platform specific issues. Please remember that consecutive writing operations may damage FFS.
This tutorial shows only basic usage of record store. Please check Java API reference for details.