`

MSRMS编程

    博客分类:
  • J2ME
阅读更多
基础知识
持久化存储的必要性
J2ME中提供了记录管理系统:RMS,和数据库很类似
相关的包:javax.microedition.rms 最重要的类:RecordStore
注意:RMS是记录管理系统,类似数据库;RecordStore是记录集,类似表,表中有Record

RecordStore
常用打开记录集的方法
  staticRecordStoreopenRecordStore(StringrecordStoreName,booleancreateIfNecessary)
  参数1:记录集名称(区分大小写);参数2:如果不存在,是否创建
  staticRecordStore openRecordStore(StringrecordStoreName,booleancreateIfNecessary,intauthmode,booleanwritable)
  参数1:记录集名称;参数2:如果不存在,是否创建;参数3:创建方式(AUTHMODE_ANY/PRIVATE),表示是否可以被其他套件创建访问;参数4:其他

套件是否可以进行写操作
  记录集占据的空间:int getSize()
  得到记录集名称:StringgetName()
  关闭记录集: closeRecordStore
  列出当前的所有记录集:RecordStore.listRecordStores();
  删除某个记录集:RecordStore.deleteRecordStore("name");

RecordStore记录操作
注意:每一个记录都有ID,第一个记录ID为1
  添加记录: int addRecord(byte[] data,intoffset,int numBytes)
  案例:将“张三”,”李四光”添加进RMS
  得到当前记录条数:int getNumRecords()
  根据ID获得记录: byte[]getRecord(int recordId)/int getRecord(int recordId, byte[] buffer,intoffset)
  根据ID获得记录字节数:int getRecordSize(int recordId)
  修改记录: voidsetRecord(int recordId, byte[]newData,intoffset,int numBytes)
  删除记录:voiddeleteRecord(int recordId) :注意,删掉一条记录之后,虽然记录数量减少了,但是记录还是保存在原来的位置,以后添加,ID在

后面增加
将对象写入RMS
  将对象转化为字节数组(序列化);
  将字节数组转化为对象

高级知识
遍历记录集
不能根据记录数量来遍历,因为纪录被删除后,其他纪录并不会填充空缺,记录数量虽然减少,但是ID数量不变
方法1:enumerateRecords(RecordFilterfilter, RecordComparatorcomparator,booleankeepUpdated) ,参数1:过滤条件,参数2:比较规则,参数3:是

否和记录集保持更新
方法2:getNextRecordID:得到下一个将要添加的记录的ID,然后用for循环,并判断记录是否为空

记录监听
  voidrecordAdded(RecordStore recordStore,int recordId) :记录添加时自动触发
  voidrecordChanged(RecordStore recordStore,int recordId) :纪录修改时自动触发
  voidrecordDeleted(RecordStore recordStore,int recordId) :记录删除时,自动触发
  RecordStore addRecordListener(RecordListenerlistener) 负责绑定
数据过滤
  RecordFilter接口
  定义一个类,实现这个接口
  重写match函数,根据情况返回true或者false
  用枚举方法来遍历时,将类的对象传进去
数据排序
  RecordComparator接口
  定义一个类,实现这个接口
  重写compare函数,根据情况返回:
      RecordComparator.PRECEDES:记录1在记录2前面
      RecordComparator.FOLLOWS:记录1在记录2后面
      RecordComparator.EQUIVALENT:记录1=记录2
  用枚举方法来遍历时,将类的对象传进去
问题
  可以修改MF或者JAD文件中的MIDlet-Data-Size属性来改变RMS的最小存储字节数,首选JAD
  文件路径在WTK上面在:用户名称\j2mewtk\2.5.2\appdb\temp.DefaultColorPhoneXX下面的db文件
  一个记录集内的容量有限,约100K,如果数据太大,考虑用多个记录集

模拟电话本
  界面出现,显示一个欢迎界面,右下角按钮:电话本;左下角按钮:退出
  按电话本按钮,显示电话本界面,用List显示所有电话;界面上左下角:返回;右下角两个按钮:添加电话,删除电话。
  按删除电话,删掉当前电话;按添加电话,进入添加电话界面,输入姓名和电话,确定,可以添加,添加完毕,进入电话本界面;该界面左下角的返回按钮供返回
package prj;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
//欢迎界面
public class WelcomeCanvas extends Canvas implements CommandListener{
	private Command cmdPhone = new Command("电话本",Command.SCREEN,1);
	private Command cmdExit = new Command("退出",Command.EXIT,1);
	private PhoneMIDlet pm;
	public WelcomeCanvas(PhoneMIDlet pm){
		this.pm = pm;
		this.addCommand(cmdPhone);
		this.addCommand(cmdExit);
		this.setCommandListener(this);
	}	
	public void commandAction(Command c,Displayable d){
		if(c==cmdPhone){
			pm.changeInterface("PhoneList");
		}
		else if(c==cmdExit){
			pm.notifyDestroyed();
		}
	}
	public void paint(Graphics g){
		String str = "欢迎光临";
		g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE));
		g.setColor(255,0,0);
		g.drawString(str, this.getWidth()/2, 50, Graphics.TOP|Graphics.HCENTER);
	}
	
}

package prj;

import java.util.Vector;

import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;

public class RMSOpe {
	private String storeName;
	private RecordStore rs;
	public RMSOpe(String storeName){
		this.storeName = storeName;
	}
	public void openRecordStore(){//打开记录集
		try{
			rs = RecordStore.openRecordStore(storeName, true);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void addPhone(String name,String phone){//添加电话
		String info = name + ":" + phone;
		byte[] b = info.getBytes();
		try{
			rs.addRecord(b, 0, b.length);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void deletePhone(String str){//删除电话
		int lastId = 0;
		try{
			lastId = rs.getNextRecordID();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		for(int i=1;i<lastId;i++){
			try{
				byte[] b = rs.getRecord(i);
				String recordStr = new String(b);
				if(recordStr.equals(str)){
					rs.deleteRecord(i);
				}
			}catch(Exception ex){}
		}
	}
	public Vector getAllPhone(){//得到所有电话
		Vector v = new Vector();
		try{
			RecordEnumeration re = rs.enumerateRecords(null,null,false);
			while(re.hasNextElement()){
				v.addElement(new String(re.nextRecord()));
			}
		}catch(Exception ex){
			ex.printStackTrace();
			}
		return v;
	}
	public void closeRecordStore(){//关闭记录集
		try{
			rs.closeRecordStore();
		}catch(Exception ex){}
		
	}
}

package prj;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

public class AddForm extends Form implements CommandListener{
	private Command cmdOK = new Command("确定添加",Command.SCREEN,1);
	private Command cmdBack = new Command("返回",Command.BACK,1);
	
	private TextField tfName = new TextField("请您输入姓名","",10,TextField.ANY);
	private TextField tfPhone = new TextField("请您输入电话","",10,TextField.PHONENUMBER);
	
	private PhoneMIDlet pm;
	public AddForm(PhoneMIDlet pm){
		super("添加电话");
		this.pm = pm;
		this.addCommand(cmdOK);
		this.addCommand(cmdBack);
		this.setCommandListener(this);
		
		this.append(tfName);
		this.append(tfPhone);
		
	}
	public void commandAction(Command c,Displayable d){
		if(c==cmdOK){//添加电话,并回到电话本界面
			//.....添加电话
			this.addPhone();
			pm.changeInterface("PhoneList");
		}
		else if(c==cmdBack){//返回电话本界面
			pm.changeInterface("PhoneList");
		}
	}
	/*添加电话*/
	public void addPhone(){
		RMSOpe rmsope = new RMSOpe("PhoneStore");
		rmsope.openRecordStore();
		rmsope.addPhone(tfName.getString(), tfPhone.getString());
		rmsope.closeRecordStore();	
	}
}

package prj;

import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

public class PhoneList extends List implements CommandListener{
	private Command cmdBack = new Command("返回",Command.BACK,1);
	private Command cmdAdd = new Command("添加电话",Command.SCREEN,1);
	private Command cmdDel = new Command("删除电话",Command.SCREEN,1);
	private PhoneMIDlet pm;
	public PhoneList(PhoneMIDlet pm){
		super("电话本",List.IMPLICIT);
		this.pm = pm;
		this.addCommand(cmdBack);
		this.addCommand(cmdAdd);
		this.addCommand(cmdDel);
		this.setCommandListener(this);
	}
	public void commandAction(Command c,Displayable d){
		if(c==cmdBack){//返回欢迎界面
			pm.changeInterface("WelcomeCanvas");
		}
		else if(c==cmdAdd){
			pm.changeInterface("AddForm");
		}
		else if(c==cmdDel){
			//删除纪录...
			this.delete();
			pm.changeInterface("PhoneList");//更新
		}
	}
	/*载入所有电话*/
	public void loadPhones(){
		RMSOpe rmsope = new RMSOpe("PhoneStore");
		rmsope.openRecordStore();
		Vector v = rmsope.getAllPhone();
		for(int i=0;i<v.size();i++){
			this.append((String)v.elementAt(i), null);
		}
		rmsope.closeRecordStore();	
		
	}
	/*删除电话*/
	public void delete(){
		RMSOpe rmsope = new RMSOpe("PhoneStore");
		rmsope.openRecordStore();
		rmsope.deletePhone(this.getString(this.getSelectedIndex()));
		rmsope.closeRecordStore();	
	}
}

package prj;

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class PhoneMIDlet extends MIDlet {
	public Display dis;
	private WelcomeCanvas wc = new WelcomeCanvas(this);
	private PhoneList pl = new PhoneList(this);
	private AddForm af = new AddForm(this);
	
	protected void startApp() throws MIDletStateChangeException {
		dis = Display.getDisplay(this);
		this.changeInterface("WelcomeCanvas");
	}
	
	public void changeInterface(String interfaceName){
		if(interfaceName.equals("WelcomeCanvas")){
			dis.setCurrent(wc);
		}
		else if(interfaceName.equals("PhoneList")){
			dis.setCurrent(pl);
			pl.deleteAll();
			pl.loadPhones();//更新
		}
		else if(interfaceName.equals("AddForm")){
			
			dis.setCurrent(af);
		}
	}
	
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics