由於軟件比較小,因此我沒有應用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
首 頁 | 新 聞 | SymBian | android| Windows Mobile | J2ME | 下載中心 | 游戲策劃 | 招聘與求職 | 購書指南 | 視頻教程
您現在的位置: 開發視界 >> J2ME >> 數據與存儲 >> 正文
基於MIDP1.0實現RMS容量探測器
作者:mingJava 文章來源:J2MEdev.com 更新時間:2006-12-1 19:48:54
956
, 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
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,
&
}
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