軟件名稱:RMS容量探測器
作者簡介:詹建飛(mingjava),北京郵電大學信息工程學院信號與信息處理專業研究生。
電子信箱:[email protected]
之所以寫這個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.companysz.com)
新聞熱點
疑難解答