Let's put some of this knowledge to work. This section details a simple MIDlet that saves a user name and password in a RecordStore. Each time the MIDlet is used, it can load the user name and password from the RecordStore instead of requiring the user to enter the same information over and over.
The MIDlet itself is very simple. Its only screen is a Form that contains fields for entering the user name and password. It uses a helper class, Preferences, to do all the RecordStore work. Listing 7-1 shows the source code for the MIDlet.
Listing 7-1: Source code for RecordMIDlet.
import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class RecordMIDlet extends MIDlet { private Preferences mPreferences; private Form mForm; private TextField mUserField, mPasswordField; public void startApp() { // Try to load the user and password from a recordstore. mPreferences = Preferences.getInstance(); if (mForm == null) { mForm = new Form("Login"); mUserField = new TextField("Name", mPreferences.getUser(), 32, 0); mPasswordField = new TextField("Password", mPreferences.getPassword(), 32, 0); mForm.append(mUserField); mForm.append(mPasswordField); mForm.addCommand(new Command("Exit", Command.EXIT, 0)); mForm.setCommandListener(new CommandListener() { public void commandAction(Command c, Displayable s) { if (c.getCommandType() == Command.EXIT) { destroyApp(true); notifyDestroyed(); } } }); } Display.getDisplay(this).setCurrent(mForm); } public void pauseApp() {} public void destroyApp(boolean unconditional) { // Save the user name and password. mPreferences.setUser(mUserField.getString()); mPreferences.setPassword(mPasswordField.getString()); mPreferences.store(); } }
All the RecordStore work is encapsulated in the Preferences class, shown in Listing 7-2. Preferences is a wrapper for a user name and password, stored as String member variables mUser and mPassword. A static method, getInstance(), provides access to a single Preferences object. Each time getInstance() is called, the user name and password are loaded from a RecordStore.
Listing 7-2: Preferences is a helper class that encapsulates RecordStore access.
import javax.microedition.rms.*; public class Preferences { private static Preferences sInstance; public static Preferences getInstance() { if (sInstance == null) sInstance = new Preferences("", ""); RecordStore rs = null; try { rs = RecordStore.openRecordStore("Preferences", true); } catch (RecordStoreException rse) { return null; } String user, password; user = password = ""; try { byte[] userBytes = rs.getRecord(1); byte[] passwordBytes = rs.getRecord(2); user = new String(userBytes); password = new String(passwordBytes); } catch (RecordStoreException rse) {} catch (NullPointerException npe) {} try { rs.closeRecordStore(); } catch (RecordStoreException rse) {} sInstance.setUser(user); sInstance.setPassword(password); return sInstance; } private String mUser, mPassword; private Preferences(String user, String password) { setUser(user); setPassword(password); } public String getUser() { return mUser; } public String getPassword() { return mPassword; } public void setUser(String user) { mUser = user; } public void setPassword(String password) { mPassword = password; } public void store() { RecordStore rs = null; try { rs = RecordStore.openRecordStore("Preferences", false); } catch (RecordStoreException rse) { return; } try { byte[] userBytes = mUser.getBytes(); byte[] passwordBytes = mPassword.getBytes(); int n = rs.getNumRecords(); if (n == 0) { rs.addRecord(userBytes, 0, userBytes.length); rs.addRecord(passwordBytes, 0, passwordBytes.length); } else { rs.setRecord(1, userBytes, 0, userBytes.length); rs.setRecord(2, passwordBytes, 0, passwordBytes.length); } } catch (RecordStoreException rse) {} try { rs.closeRecordStore(); } catch (RecordStoreException rse) {} } }
RecordMIDlet saves the updated values back to the RecordStore in its destroyApp() method. It saves the user name and password from the user interface in the Preferences object, then calls the store() method to write the new values out to the RecordStore. The store() method simply adds the records if the RecordStore is empty, or sets the existing records to the new values.
To test out the MIDlet, enter some text into the user name and password fields. Then exit the MIDlet and restart it. You will see the same values loaded into the text fields.
Team-Fly |