程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> 基於MIDP1.0實現RMS容量探測器

基於MIDP1.0實現RMS容量探測器

編輯:J2ME
之所以寫這個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

首 頁 | 新 聞 | 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

= new Command("退出", Command.EXIT, 3);
    public static final int INC = 1;
    public TimerTask timerTask;
    public final Timer timer = new Timer();

    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,
         &

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved