软件名称:RMS容量探测器
作者简介:詹建飞(mingjava),北京邮电大学信息工程学院信号与信息处理专业研究生。
电子信箱:eric.zhan@263.net
之所以写这个RMS容量探测器是因为我在nokia 6108手机上编写个人通信录的时候想知道它的RecordStore最大有多少空间可以使用。我调用getSizeAviable()的时候发现它返回的值几乎等于手机上剩余的内存空间的值,因此我想这个API的实现是错误的。于是编写了这个简单的RMS容量探测器,它能探测手机上一个RecordStore能存储数据的最大值,单位是K字节,请不要在模拟器上运行这个软件,这没有任何意义。下面简单介绍一下实现。
由于软件比较小,因此我没有应用MVC的设计模式,如果编写较大的应用程序一定要使用MVC。软件一共有三个类:CounterCanvas,RMSAnalyzer和RMSModel。软件的原理是:新建一个RecordStore然后每隔100ms往里面写入1K的数据,在退出的时候删除这个RecordStore。思路非常简单!代码也不复杂,因此我直接给出源代码。你可以从这里下载源文件(其中包括jar文件和jad文件)。如果提供下载请注明出处和作者
import javax.microedition.rms.*;
public class RMSModel
{
public static final int K = 1024;
PRivate RecordStore rs;
private int baseCount;
private RMSAnalyzer RMSanalyzer;
public static final String name = "test";
public RMSModel(int baseCount, RMSAnalyzer rmsa)
throws RecordStoreException
{
this.baseCount = baseCount;
this.RMSanalyzer = rmsa;
if (rs == null)
{
rs = RecordStore.openRecordStore(name, true);
writeRecord(baseCount);
}
}
public void writeRecord(int count) throws RecordStoreException
{
byte[] data = new byte[count * K];
for (int i = 0; i < count; i++)
{
data[i] = 1;
}
rs.addRecord(data, 0, count * K);
}
public void deleteRMS()
{
try
{
rs.closeRecordStore();
RecordStore.deleteRecordStore(name);
} catch (RecordStoreException e)
{
RMSanalyzer.showAlertError(e.getMessage());
}
}
}
import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordStoreException;
public class RMSAnalyzer extends MIDlet implements CommandListener
{
private Display display;
private CounterCanvas counterCanvas;
private Alert alert;
public static final Command startCommand = new Command("开始", Command.ITEM,
1);
public static final Command exitCommand = new Command("退出", Command.EXIT, 2);
protected void startApp() throws MIDletStateChangeException
{
display = Display.getDisplay(this);
alert = new Alert("错误提示");
try
{
String interval = this.getAppProperty("INTER");
int t = Integer.parseInt(interval);
counterCanvas = new CounterCanvas(t, 0, this);
} catch (RecordStoreException e)
{
this.showAlertError(e.getMessage());
}
Form mainForm = new Form("RMS测试器");
Image mainImage = getMainImage("java.png");
if (mainImage != null)
{
mainForm.append(mainImage);
} else
{
mainForm.append("欢迎使用自由软件");
}
mainForm.addCommand(startCommand);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
display.setCurrent(mainForm);
}
public Image getMainImage(String name)
{
Image image = null;
try
{
image = Image.createImage("/" + name);
} catch (IOException e)
{
return null;
}
return image;
}
public Display getDisplay()
{
return display;
}
protected void pauseApp()
{
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
}
public void commandAction(Command cmd, Displayable disp)
{
if (cmd == startCommand)
{
display.setCurrent(counterCanvas);
counterCanvas.start();
} else if (cmd == exitCommand)
{
exitMIDlet();
}
}
public void exitMIDlet()
{
try
{
destroyApp(false);
notifyDestroyed();
} catch (MIDletStateChangeException e)
{
showAlertError(e.getMessage());
}
}
public void showAlertError(String message)
{
alert.setString(message);
alert.setType(AlertType.ERROR);
alert.setTimeout(2500);
display.setCurrent(alert);
}
}
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.rms.*;
public class CounterCanvas extends Canvas implements CommandListener
{
private RMSModel model;
private RMSAnalyzer RMSanalyzer;
private int interTime;
private int counter;
private boolean go = true;
public static Command exitCommand = new Comman
public CounterCanvas(int interTime, int base, RMSAnalyzer rmsa)
throws RecordStoreException
{
this.interTime = interTime;
this.counter = base;
this.RMSanalyzer = rmsa;
model = new RMSModel(base, RMSanalyzer);
this.addCommand(exitCommand);
this.setCommandListener(this);
timerTask = new TimerTask()
{
public void run()
{
try
{
model.writeRecord(INC);
counter++;
} catch (RecordStoreFullException e)
{
go = false;
model.deleteRMS();
timer.cancel();
} catch (RecordStoreException e)
{
model.deleteRMS();
RMSanalyzer.showAlertError(e.getMessage());
timer.cancel();
}
repaint();
}
};
}
public void start()
{
timer.schedule(timerTask, 500, interTime);
}
public void setCounter(int counter)
{
this.counter = counter;
}
public void setInterTime(int interTime)
{
this.interTime = interTime;
}
protected void paint(Graphics arg0)
{
int SCREEN_WIDTH = this.getWidth();
int SCREEN_HEIGHT = this.getHeight();
arg0.drawRect(SCREEN_WIDTH / 10, SCREEN_HEIGHT / 2,
SCREEN_WIDTH * 4 / 5, 10);
if (RMSanalyzer.getDisplay().isColor())
{
arg0.setColor(128, 128, 255);
}
arg0.fillRect(SCREEN_WIDTH / 10 + 1, SCREEN_HEIGHT / 2 + 1, counter, 9);
if (!go)
arg0.drawString("最大值:" + counter + "K字节", SCREEN_WIDTH / 10,
SCREEN_HEIGHT / 10, Graphics.TOP Graphics.LEFT);
}
public void commandAction(Command arg0, Displayable arg1)
{
if (arg0 == exitCommand)
{
RMSanalyzer.exitMIDlet();
}
}
}
RMSAnalyzer.jad
MIDlet-Jar-Size: 15785
MIDlet-1: RMSAnalyzer,,RMSAnalyzer
MIDlet-Jar-URL: RMSAnalyzer.jar
MicroEdition-Configuration: CLDC-1.0
MIDlet-Version: 1.0.0
MIDlet-Name: RMSAnalyzer
MIDlet-Data-Size: 8192
MIDlet-Vendor: www.j2medev.com
MicroEdition-Profile: MIDP-1.0
INTER: 100
(出处:http://www.VeVb.com)
新闻热点
疑难解答