Slide 4.6: Programming with the RMS (cont.)
  Slide 4.8: Preferences.java
  Home


RecordMIDlet.java


This MIDlet 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. 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.

 RecordMIDlet.java 

import  javax.microedition.midlet.*;
import  javax.microedition.lcdui.*;
import  javax.microedition.rms.RecordStoreException;

public class  RecordMIDlet
    extends  MIDlet
    implements  CommandListener {
  private static final String  kUser     = "user";
  private static final String  kPassword = "password";
  
  private Preferences  mPreferences;
  private Form         mForm;
  private TextField    mUserField, mPasswordField;
  
  public  RecordMIDlet( ) {
    try {
      mPreferences = new Preferences( "preferences" );
    }
    catch ( RecordStoreException  rse ) {
      mForm = new Form( "Exception" );
      mForm.append( new StringItem( null, rse.toString( ) ) );
      mForm.addCommand( new Command( "Exit", Command.EXIT, 0 ) );
      mForm.setCommandListener( this );
      return;
    }

    mForm = new Form( "Login" );
    mUserField     = new TextField(
      "Name", mPreferences.get( kUser ), 32, 0 );
    mPasswordField = new TextField(
      "Password", mPreferences.get( kPassword ), 32, 0 );
    mForm.append( mUserField );
    mForm.append( mPasswordField );
    mForm.addCommand( new Command( "Exit", Command.EXIT, 0 ) );
    mForm.setCommandListener( this );
  }
  
  public void  startApp( ) {
    Display.getDisplay( this ).setCurrent( mForm );
  }
  
  public void  pauseApp( ) { }

  public void  destroyApp( boolean unconditional ) {
    // Save the user name and password.
    mPreferences.put( kUser, mUserField.getString( ) );
    mPreferences.put( kPassword, mPasswordField.getString( ) );
    try { 
      mPreferences.save( );
    }
    catch ( RecordStoreException  rse ) { }
  }

  public void  commandAction( Command c, Displayable s ) {
    if ( c.getCommandType( ) == Command.EXIT ) {
      destroyApp( true );
      notifyDestroyed( );
    }
  }
}


Click on Launch ——>


<—— Click on Exit