📄 rmsoperations.java
字号:
/*
* Filename: RMSOperations.java
* - contains definition of the class RMSOperations. It is constructed for managing read/write operations
* of Record Store on phone.
*
*
* Application Name: Image Album Demo
* Version: 2.0
* Release Date: 27th September, 2002
* Author: Edmund Wong, Application Engineer, Metrowerks Hong Kong
*
*
* (c) Copyright. 2002. Metrowerks Corp. ALL RIGHTS RESERVED.
* This code is provided "AS IS" without warranty of any kind and subject to Metrowerks Sample Application
* End User License Agreement. NO EXTERNAL DISTRIBUTION OR COMMERCIALIZATION OF THE SOFTWARE IS PERMITTED
* EXCEPT BY WRITTEN AGREEMENT OF METROWERKS."
*/
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class RMSOperations {
/**
* create member variables for class RMSOperations
*/
RecordStore rs = null; // create RecordStore for RMS operation
RecordEnumeration rsEnumerate = null; // create RecordEnumeration for RMS operation
boolean enumerate; // true if need to geneate RecordEnumeration
int x; // for storing temporary record ID value returned
public Alert saveOKAlert; // create reference for saveOK alert for dispalying 'Save OK' message
public Alert saveFailAlert; // create reference for saveFail alert for dispalying 'Save Fail' message
byte[] byteArray; // temporary data buffer
/**
* Constructor for RMSOperations
*/
public RMSOperations(){
}
/**
* Method for opening RecordStore with the specified name, create RecordEnumeration if specified
*/
public void openRecordStore(String recordStoreName, boolean enumerate) {
this.enumerate = enumerate;
try{
rs = RecordStore.openRecordStore(recordStoreName, true);
}
catch(Exception e) {
System.out.println("Exception: " + e.toString());
}
if (enumerate == true) {
try{
rsEnumerate = rs.enumerateRecords(null, null, true);
rsEnumerate.reset();
}
catch(Exception e){
System.out.println("Exception: " + e.toString());
}
}
}
/**
* Method for closing the currently opened RecordStore
*/
public void closeRecordStore() {
try{
rs.closeRecordStore();
rs = null;
}
catch(Exception e) {
System.out.println("Exception: " + e.toString());
}
if (enumerate == true) {
rsEnumerate.destroy();
}
}
/**
* Method for getting the RecordID of the next record in the current RecordEnumeration
*/
public int getNextRecordID() {
try{
x = rsEnumerate.nextRecordId();
}
catch(Exception e){
System.out.println("Exception: " + e.toString());
}
System.out.println("next Record ID " + x);
return x;
}
/**
* Method for getting the RecordID of the previous record in the current RecordEnumeration
*/
public int getPreviousRecordID() {
try{
x = rsEnumerate.previousRecordId();
}
catch(Exception e){
System.out.println("Exception: " + e.toString());
}
System.out.println("previous Record ID " + x);
return x;
}
/**
* Method for getting the Record with the specified Record ID in the current opened RecordStore
*/
public byte[] getRecord(int recordID){
try {
byteArray = rs.getRecord(recordID);
}
catch(Exception e){
System.out.println("Exception: " + e.toString());
}
System.out.println("get Record " + recordID);
return byteArray; //returning byteArray with byte data of the record retrieved
}
/**
* Method for storing the byte data in the byteArray into a new record in the currently opened RecordStore
*/
public int addRecord(byte[] byteArray){
try {
x = rs.addRecord(byteArray, 0, byteArray.length);
}
catch(Exception e){
System.out.println("Exception: " + e.toString());
}
System.out.println("add Record " + x);
return x; //returning record ID of the record added to RecrodStore
}
/**
* Method for getting the no. of records in the currently opened RecordStore
*/
public int getNumberOfRecords(){
try{
x = rs.getNumRecords();
}
catch(Exception e){
System.out.println("Exception: " + e.toString());
}
System.out.println("no .of Record " + x);
return x;
}
/**
* Method for getting the size used by the currently opened RecordStore
*/
public int getSize(){
try{
x = rs.getSize();
}
catch(Exception e){
System.out.println("Exception: " + e.toString());
}
return x;
}
/**
* Method for getting the size available for RecordStore to use
*/
public int getSizeAvailable(){
try{
x = rs.getSizeAvailable();
}
catch(Exception e){
System.out.println("Exception: " + e.toString());
}
return x;
}
/**
* Method for getting the record ID of the n th sequential record in the RecordStore
*/
public int getRecordIDAtSequenceNo(int sequenceNo) {
rsEnumerate.reset();
for (int i=1; i <= sequenceNo; i++){
try {
//x = rsEnumerate.previousRecordId(); // for SEMC phone
x = rsEnumerate.nextRecordId(); // for Motorola phone
System.out.println("Record ID " + x);
}
catch(Exception e){
System.out.println("Exception: " + e.toString());
}
}
System.out.println("record of the " + sequenceNo + " th at record ID "+ x);
return x;
}
/**
* Method for deleting the Record with the specified RecordID
*/
public void deleteRecord(int recordID) {
try{
rs.deleteRecord(recordID);
}
catch(Exception e){
System.out.println("Exception: " + e.toString());
}
System.out.println("delete Record ID " + recordID);
}
/**
* Method for updating the Record with the specified RecordID and byte data in byteArray
*/
public void setRecord(int recordID, byte[] byteArray, String message, Displayable s) { // displaying message in
// OK alert and then display
// next displayable
try{
rs.setRecord(recordID, byteArray, 0, byteArray.length);
displaySaveOKAlert(message, s);
//ImageAlbum.myDisplay.setCurrent(ImageAlbum.saveOKAlert, s);
}
catch(Exception e){
System.out.println("Exception: " + e.toString());
}
}
/**
* Method for displaying 'Save OK' notification to user, with specified screen after showing the message
*/
public void displaySaveOKAlert(String message, Displayable s){
// Alert message for successful save operation
saveOKAlert = new Alert("Save OK!", message, null, AlertType.CONFIRMATION);
saveOKAlert.setTimeout(2500);
ImageAlbum.myDisplay.setCurrent(saveOKAlert, s);
saveOKAlert=null;
}
/**
* Method for displaying 'Save Fail' notification to user, with specified screen after showing the message
*/
public void displaySaveFailAlert(String message, Displayable s){
// Alert message for successful save operation
saveFailAlert = new Alert("Save Failed!", message, null, AlertType.ERROR);
saveFailAlert.setTimeout(Alert.FOREVER);
ImageAlbum.myDisplay.setCurrent(saveFailAlert, s);
saveFailAlert=null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -