應用RMS實現用戶自動登陸功能

MIDP的子系統Record Management System提供了MIDlet的持久性存儲,精通MIDP子系統RMS系列文章對其使用進行了詳細介紹。本文講述如何使用RMS提供的功能實現應用程序的定製功能——自動登陸。

    我們的設計思路非常簡單,在RecordStore中存儲用戶的設置和用戶的信息(用戶名和密碼),如果用戶選擇自動登陸的話,那麼下次當用戶想聯網的時候將跳過登陸界面,系統會從RecordStore中讀取用戶和密碼,經過服務器的驗證後轉入到適當的界面。我對整個程序進行了簡化,我們不進行聯網,對信息的存儲也都從簡,只是爲了說明RMS實現應用程序定製的思路,因此給出的代碼並沒有全面測試和優化。下面是程序的截圖2004114211230512.gif

   

 

 

 

 





 

 

     我們用Account和Preference分別存儲用戶信息和用戶的個性化設置,同樣在這兩個類中提供序列化的方法,這樣方便我們從RecordStore中讀取和寫入。這裏只給出Preference類的代碼,Account類似。
package com.j2medev.autologin;

import java.io.*;

public class Preference
{
    private boolean autoLogin;

    public Preference(boolean _autoLogin)
    {
        this.autoLogin = _autoLogin;
    }

    public Preference()
    {
    }

    public void serialize(DataOutputStream dos) throws IOException
    {
        dos.writeBoolean(autoLogin);
    }

    public static Preference deserialize(DataInputStream dis)
            throws IOException
    {
        Preference preference = new Preference();
        preference.setAutoLogin(dis.readBoolean());

        return preference;
    }

    public boolean isAutoLogin()
    {
        return autoLogin;
    }

    public void setAutoLogin(boolean autoLogin)
    {
        this.autoLogin = autoLogin;
    }
}

    我們需要一個Model類來處理讀取和寫入RecordStore數據的邏輯,它也非常簡單。爲了簡化程序我們規定首先寫入Account然後寫入Preference,這樣我們讀取的時候只要通過recordID分別爲1和2來讀取了,在實際使用的時候通常會比較複雜,我們要藉助過濾器等查找,可以參考我的基於MIDP1.0實現個人通訊錄。

package com.j2medev.autologin;

import javax.microedition.rms.*;
import java.io.*;


public class Model
{
    private RecordStore accountStore;
    public static final String RNAME = "accountstore";

    public Model()
    {
        try
        {
            accountStore = RecordStore.openRecordStore(RNAME, true);
        } catch (RecordStoreException e)
        {
            e.printStackTrace();
        }
    }

    public void closeRecordStore()
    {
        try
        {
            accountStore.closeRecordStore();
        } catch (RecordStoreException e)
        {
            e.printStackTrace();
        }
    }

    public void saveAccount(Account account)
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        try
        {
            account.serialize(dos);
            byte[] data = baos.toByteArray();
            accountStore.addRecord(data, 0, data.length);
            baos.close();
        } catch (IOException e)
        {
            e.printStackTrace();
        } catch (RecordStoreException e)
        {
            e.printStackTrace();
        }

    }

    public Account getAccount(int recordID)
    {

        try
        {
            if (accountStore.getNumRecords() > 0)
            {
                byte[] data = accountStore.getRecord(recordID);
                ByteArrayInputStream bais = new ByteArrayInputStream(data);
                DataInputStream dis = new DataInputStream(bais);
                Account account = Account.deserialize(dis);
                bais.close();
                return account;
            }
            return null;

        } catch (IOException e)
        {
            return null;
        } catch (RecordStoreException e)
        {
            return null;
        }
    }

    public void savePreference(Preference preference)
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        try
        {
            preference.serialize(dos);
            byte[] data = baos.toByteArray();
            accountStore.addRecord(data, 0, data.length);
            baos.close();
           
        } catch (IOException e)
        {
            e.printStackTrace();
        } catch (RecordStoreException e)
        {
            e.printStackTrace();
        }

    }

    public Preference getPreference(int recordID)
    {
        try
        {
            if (accountStore.getNumRecords() > 0)
            {
                byte[] data = accountStore.getRecord(recordID);
                ByteArrayInputStream bais = new ByteArrayInputStream(data);
                DataInputStream dis = new DataInputStream(bais);
                Preference preference = Preference.deserialize(dis);
                bais.close();
                return preference;
            }
            return null;
        } catch (IOException e)
        {
            return null;
        } catch (RecordStoreException e)
        {
            return null;
        }
    }

}

    MIDlet的設計同樣很簡單,直接給出代碼。整個程序的代碼可以從這裏下載

package com.j2medev.autologin;

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


public class LoginMIDlet extends MIDlet implements CommandListener
{

    private Display display;
    private Form loginForm;
    private Form successForm;
    private TextField userName;
    private TextField password;
    private ChoiceGroup autoLogin;
    private Model model;
    public static final Command ConnCMD = new Command("Connect", Command.OK, 1);

    protected void startApp() throws MIDletStateChangeException
    {
        initMIDlet();
        Preference p = model.getPreference(2);
        if (p == null || !p.isAutoLogin())
        {
            display.setCurrent(loginForm);
        } else if (p.isAutoLogin())
        {
            Account account = model.getAccount(1);
            System.out.println("username:" + account.getUsrName() + "password:"
                    + account.getPassword());
            display.setCurrent(successForm);

        }

    }

    public void initMIDlet()
    {
        model = new Model();
        display = Display.getDisplay(this);
        loginForm = new Form("LoginForm");
        userName = new TextField("username", null, 20, TextField.ANY);
        password = new TextField("password", null, 20, TextField.PASSWORD);
        autoLogin = new ChoiceGroup("AutoLogin", Choice.MULTIPLE,
                new String[] { "remember me" }, null);
        loginForm.append(userName);
        loginForm.append(password);
        loginForm.append(autoLogin);
        loginForm.addCommand(ConnCMD);
        loginForm.setCommandListener(this);
        successForm = new Form("OK");
        successForm.append("Ok you have connected to server");
    }

    protected void pauseApp()
    {
     
    }


    protected void destroyApp(boolean arg0) throws MIDletStateChangeException
    {

    }

    public void commandAction(Command arg0, Displayable arg1)
    {
        String _userName;
        String _password;
        boolean auto = false;

        if (arg0 == ConnCMD)
        {
            _userName = userName.getString();
            _password = password.getString();
            auto = autoLogin.isSelected(0);
            System.out.println(_userName + _password + auto);
            if (auto)
            {
                Account account = new Account(_userName, _password);
                model.saveAccount(account);
                Preference preference = new Preference(auto);
                model.savePreference(preference);

            }
            display.setCurrent(successForm);
        }
    }

}

BTW:上邊的設計中對recordID考慮得不是很完善,實際編寫代碼的時候應該修正。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章