⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 recordid2midlet.java

📁 java移动通信程序设计
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
// A first MIDlet with simple text and a few commands.
public class RecordID2MIDlet extends MIDlet
implements CommandListener,RecordListener {
  private Command exitCommand;
    //The display for this MIDlet
  private Display display;

  RecordStore mainRS = null;
  RecordStore backupRS = null;

  public RecordID2MIDlet() {
    display = Display.getDisplay(this);
    exitCommand = new Command("离开", Command.EXIT, 1);
   }

  // Start the MIDlet by creating the TextBox and
  // associating the exit command and listener.
  public void startApp() {
	TextBox aTextBox = new TextBox("主画面",null,256,TextField.ANY);

	byte[] nameEmail=null;
	boolean existingOrNot = false;
	boolean OK = true;

	existingOrNot = existing("aRS1");
	if(existingOrNot){
	   try{
	       mainRS = RecordStore.openRecordStore("aRS1",false);
               mainRS.addRecordListener(this); 
           }
            catch(Exception e){
		OK = false;
	   }
	   finally{
		 if(OK){
			 aTextBox.setString("aRS已存在并且已打开");
	          }
	          else{
			aTextBox.setString("aRS已存在但无法打开");
	          }
            }
        }
        else{
	try{
	    mainRS = RecordStore.openRecordStore("aRS1",true);
	    mainRS.addRecordListener(this);
           }
             catch(Exception e){
		OK = false;
	    }
	    finally{
		if(OK){
	    	aTextBox.setString("aRS虽未存在,但已创建并完成打开的操作!");
		}
		else{
	    	aTextBox.setString("aRS虽未存在,但无法创建,打开文件失败!");
		}
	}
	}

    if(OK)
       try{
	    nameEmail = "JIDCA=login@ms16.hinet.net".getBytes();

  	    mainRS.addRecord(nameEmail,0,nameEmail.length);
            aTextBox.setString("已完成新增记录的操作!");
		    }
	   catch(Exception e){
			aTextBox.setString("新增记录的操作失败!");
	   }
        String result = new String(nameEmail);
        int position = result.indexOf('=');
        result = "姓名:"+result.substring(0,position)+"\n"+"E-mail:"+result.substring(position+1,result.length());   
        aTextBox.setString(result);

      try{
	  int recordID = mainRS.getNextRecordID()-1;
          byte[] newNameEmail = "Logis=login@ms16.hinet.net".getBytes();
          mainRS.setRecord(recordID,newNameEmail,0,newNameEmail.length);
	  System.out.println("Record :"+recordID+"have been changed and backup.");
       }
      catch(Exception e){
          System.out.println("testing 1...");
       }

      try{
       	  int recordID = mainRS.getNextRecordID()-1;
          mainRS.deleteRecord(recordID);   
	  System.out.println("Record :"+recordID+"have been deleted.");
       }
      catch(Exception e){
          System.out.println("testing 2...");
       }

    aTextBox.addCommand(exitCommand);
    aTextBox.setCommandListener(this);
    display.setCurrent(aTextBox);
  }

  // Pause is a no-op because there are no background
  // activities or record stores to be closed.
  public void pauseApp() { }

  public void destroyApp(boolean unconditional) {
    try{
        if (mainRS!=null){
           mainRS.closeRecordStore(); 
       }
    }
    catch (Exception e){
    }
    try{
        if (backupRS!=null){
            backupRS.closeRecordStore();
       }
    }
    catch (Exception e){
    }
  }
  
  public boolean existing(String recordStoreName) {
    boolean existingOrNot = false;
    RecordStore mainRS = null;
    if(recordStoreName.length() > 32) return false;
    try{
    	mainRS = RecordStore.openRecordStore(recordStoreName,false);
    }
    catch(RecordStoreNotFoundException e){
        existingOrNot = false;
    }
    catch(Exception e){
    }
    finally{
    	try{
	    mainRS.closeRecordStore();
	    }
	    catch(Exception e){
	    }
    }
    return existingOrNot;
  }

  public void commandAction(Command c, Displayable s) {
     destroyApp(false);
      notifyDestroyed();
  }

  public void recordAdded(RecordStore recordStore,int recordID){
    if (recordStore == mainRS) {
     try{
         byte[] data = recordStore.getRecord(recordID);
         int recID = backupRS.addRecord(data,0,data.length);
         System.out.println("New Record:"+recID+"have been backup.");
     }      
     catch(Exception e){
     }
    }
  }

  public void recordChanged(RecordStore recordStore,int recordID){
    if (recordStore == mainRS) {
     try{
         byte[] data = recordStore.getRecord(recordID);
         backupRS.setRecord(recordID,data,0,data.length);
         System.out.println("Changed Record:"+recordID+"have been backup.");
     }      
     catch(Exception e){
     }
    }
  }

  public void recordDeleted(RecordStore recordStore,int recordID){
    if (recordStore == mainRS) {
     try{
         backupRS.deleteRecord(recordID);
         System.out.println("Rest Records have been backup.");
     }      
     catch(Exception e){
     }
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -