📄 listenerrms.java
字号:
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;
/**
* An example of how to use the MIDP 1.0 Record Management System (RMS)
* Listener interface.
* @author Martin J. Wells
*/
public class ListenerRMS extends MIDlet
{
/**
* An inner class which implements the RecordListener interface.
*/
class Listener implements RecordListener
{
/**
* Called when a record is added to a record store.
* @param recordStore the store it was added to
* @param i the record index it was assigned
*/
public void recordAdded(RecordStore recordStore, int i)
{
try
{
System.out.println("Record " + i + " added to " + recordStore.getName());
}
catch(Exception e)
{
System.out.println(e);
}
}
/**
* Called when an existing record was changed.
* @param recordStore the store where the record resided
* @param i the index of the record
*/
public void recordChanged(RecordStore recordStore, int i)
{
try
{
System.out.println("Record " + i + " changed in " + recordStore.getName());
}
catch (Exception e)
{
System.out.println(e);
}
}
/**
* Called when an exsiting record is deleted.
* @param recordStore the store it was deleted from
* @param i the index of the record that was deleted
*/
public void recordDeleted(RecordStore recordStore, int i)
{
try
{
System.out.println("Record " + i + " deleted from " + recordStore.getName());
}
catch (Exception e)
{
System.out.println(e);
}
}
}
private RecordStore rs;
private static final String STORE_NAME = "My Record Store";
/**
* Constructor for the demonstration MIDlet does all the work for the tests.
* It firstly opens (or creates if required) a record store and then inserts
* some records containing data. It then reads those records back and
* displays the results on the console. The associated Listener object will
* output messages as this process runs.
* @throws Exception
*/
public ListenerRMS() throws Exception
{
// Open (and optionally create a record store for our data
rs = RecordStore.openRecordStore(STORE_NAME, true);
rs.addRecordListener(new Listener());
// Create some records in the store
String[] words = {"they", "mostly", "come", "at", "night"};
for (int i=0; i < words.length; i++)
{
// Create a byte stream we can write to
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
// To make life easier use a DataOutputStream to write the bytes
// to the byteStream (ie. we get the writeXXX methods)
DataOutputStream dataOutputStream = new DataOutputStream(byteOutputStream);
dataOutputStream.writeUTF(words[i]);
// ... add other dataOutputStream.writeXXX statements if you like
dataOutputStream.flush();
// add the record
byte[] recordOut = byteOutputStream.toByteArray();
int newRecordId = rs.addRecord(recordOut, 0, recordOut.length);
System.out.println("Adding new record: " + newRecordId +
" Value: " + recordOut.toString());
dataOutputStream.close();
byteOutputStream.close();
}
// retrieve the state of the store now that it's been populated
System.out.println("Record store now has " + rs.getNumRecords() +
" record(s) using " + rs.getSize() + " byte(s) " +
"[" + rs.getSizeAvailable() + " bytes free]");
// retrieve the records
for (int i=1; i <= rs.getNumRecords(); i++)
{
int recordSize = rs.getRecordSize(i);
if (recordSize > 0)
{
// construct a byte and wrapping data stream to read back the
// java types from the binary format
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(rs.getRecord(i));
DataInputStream dataInputStream = new DataInputStream(byteInputStream);
String value = dataInputStream.readUTF();
// ... add other dataOutputStream.readXXX statements here matching the
// order they were written above
System.out.println("Retrieved record: " + i + " Value: " + value);
dataInputStream.close();
byteInputStream.close();
}
}
}
/**
* Called by the Application Manager when the MIDlet is starting or resuming
* after being paused. In this case we just exit as soon as we start.
* @throws MIDletStateChangeException
*/
protected void startApp() throws MIDletStateChangeException
{
destroyApp(false);
notifyDestroyed();
}
/**
* Called by the MID's Application Manager to pause the MIDlet. A good
* example of this is when the user receives an incoming phone call whilst
* playing your game. When they're done the Application Manager will call
* startApp to resume. For this example we don't need to do anything.
*/
protected void pauseApp()
{
}
/**
* Called by the MID's Application Manager when the MIDlet is about to
* be destroyed (removed from memory). You should take this as an opportunity
* to clear up any resources and save the game. For this example we don't
* need to do anything.
* @param unconditional if false you have the option of throwing a
* MIDletStateChangeException to abort the destruction process.
* @throws MIDletStateChangeException
*/
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -