Application Development for Mobile Devices Lecture5 - Peristence, Study notes of Mobile Computing

Description about Java ME – Storing data on the device, Optional versus core APIs, Three ways of storing data , Using Record Stores,File and PIM, Deleting a RecordStore.

Typology: Study notes

2010/2011

Uploaded on 09/08/2011

rossi46
rossi46 🇬🇧

4.5

(10)

313 documents

1 / 44

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 5
Java ME – Storing data on the
device
S.Kapetanakis & Markus A. Wolf
Based on material from Gill Windall
1
Application Development
for Mobile Devices
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c

Partial preview of the text

Download Application Development for Mobile Devices Lecture5 - Peristence and more Study notes Mobile Computing in PDF only on Docsity!

Lecture 5

Java ME – Storing data on the

device

S.Kapetanakis & Markus A. Wolf

Based on material from Gill Windall

1 Application Development for Mobile Devices

Contents Optional versus core APIs Three ways of storing data Using Record Stores Introduction Opening, closing and writing to a RecordStore Reading data from a RecordStore Deleting a RecordStore Things we aren't studying and limitations of RecordStores File and PIM

Three ways for MIDlets to store data

Record Stores

Sort of limited mini-databases

Only accessible from MIDlets

A core API

Files

Similar to files on a PC

Accessible to non-MIDlets as well

An optional API

PIM (Personal Information Management) stores

Special format for contacts, diary etc

Accessible to other applications

An optional API 4

Why do we need the ability to store

data on a mobile device?

So applications can work in an

offline/occasionally connected mode:

collect data offline then upload download data then disconnect

Store user preferences

Databases of reference data

Improve performance by reducing network

roundtrips

How an application sees a Record

Store

A small database containing pieces of data

called records

Accessed via the class

javax.microedition.rms.RecordStore

In MIDP 1.0 Record Stores cannot be shared

between MIDlet suites but in MIDP 2.0 they can

Each record store has a name that is unique

within a MIDlet suite

MIDlet Suite MIDlet MIDlet MIDlet MIDlet Suite MIDlet RecordStore RecordStore RecordStore Persistent Storage on the Device

Record Stores belong to MIDlet suites

Opening a RecordStore Before you can do anything with a RecordStore you must open it RecordStore rs = RecordStore.openRecordStore("Details", true); name of the RecordStore true means that if the RecordStore doesn't already exist it'll be created If anything goes wrong when you're accessing a RecordStore the code may "throw an exception"

Writing a Record Records are written as arrays of bytes It's easy to convert from a String to an array of bytes String detailsToAdd = strName + ";" + strAge + ";" + strEmail; byte [] detailsBuffer = detailsToAdd.getBytes(); rs.addRecord(detailsBuffer, 0, detailsBuffer.length); make up a String to hold the details to be written convert to an array of bytes write the data (^) amount to write name of the array start of data in the array

What's in our RecordStore? Details 4 mickey ; 60 ; [email protected] 5 gill ; 23 ; [email protected] 7 sati ; 24 ; [email protected]

  • (^) Let's assume we've added three records to the "Details" RecordStore
  • (^) Remember that the system allocates the record ids. They will probably be consecutive but we can't be sure

Closing a RecordStore Closing a RecordStore will release the resources, including memory, associated with it There's a trade-off between keeping a RecordStore open for the life of your MIDlet - saves the overhead of repeatedly opening and closing it opening and closing a RecordStore in your MIDlet each time you use it - minimises use of memory To close a RecordStore rs.closeRecordStore();

Finding the number of Records ....... rs.addRecord(detailsBuffer, 0, detailsBuffer.length); // find out the number of records int recordCount = rs.getNumRecords(); rs.closeRecordStore(); // display confirmation to the user mAlertConfirmDetailsSaved.setString("** Details saved ** \n" + recordCount + " records have been stored" ); ........ Persist2.java

Persist2.java import javax.microedition.midlet.; import javax.microedition.lcdui.; import javax.microedition.rms.RecordStore; public class Persist2 extends MIDlet implements CommandListener { private Display mDisplay; private Alert mAlertConfirmDetails, mAlertConfirmDetailsSaved; private Form mDataEntryForm; private TextField txtName, txtAge, txtEmail; private String strName, strAge, strEmail; public Persist2() { // create the Form mDataEntryForm = new Form("Enter your details"); txtName = new TextField("Name:", null, 15, TextField.ANY); txtAge = new TextField("Age:", null, 3, TextField.NUMERIC); txtAge.setLayout(Item.LAYOUT_2 | Item.LAYOUT_CENTER); txtEmail = new TextField("Email:", null, 20, TextField.EMAILADDR);

protected void startApp() { mDisplay = Display.getDisplay(this); mDisplay.setCurrent(mDataEntryForm); } public void commandAction(Command c, Displayable d) { if (c.getLabel().equals("Exit")) { notifyDestroyed(); } if (c.getLabel().equals("Back")) { mDisplay.setCurrent(mDataEntryForm); } if (c.getLabel().equals("Next") && d == mDataEntryForm) { // get the details entered strName = txtName.getString(); strAge = txtAge.getString(); strEmail = txtEmail.getString(); // display the details to the user mAlertConfirmDetails.setString("Details entered: \n " + strName + "\n " + strAge + "\n " + strEmail); mDisplay.setCurrent(mAlertConfirmDetails); }

if (c.getLabel().equals("Save")) { // write the details to the database try { RecordStore rs = RecordStore.openRecordStore("Details", true); String detailsToAdd = strName + ";" + strAge + ";" + strEmail; byte [] detailsBuffer = detailsToAdd.getBytes(); rs.addRecord(detailsBuffer, 0, detailsBuffer.length); // find out the number of records int recordCount = rs.getNumRecords(); rs.closeRecordStore(); // display confirmation to the user and clear the form mAlertConfirmDetailsSaved.setString(" ** Details saved **\n " + recordCount + " records have been stored"); // clear the form ready for redisplay txtName.setString(""); txtAge.setString(""); txtEmail.setString(""); } catch (Exception e) { // an error occurred saving mAlertConfirmDetailsSaved.setString("Couldn't save details"); System.err.println("Error saving to database"); } mDisplay.setCurrent(mAlertConfirmDetailsSaved, mDataEntryForm); } }