国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

應用RMS實現(xiàn)用戶自動登陸功能解析

2019-11-18 12:25:45
字體:
供稿:網(wǎng)友

  MIDP的子系統(tǒng)Record Management System提供了MIDlet的持久性存儲,精通MIDP子系統(tǒng)RMS系列文章對其使用進行了具體介紹。本文講述如何使用RMS提供的功能實現(xiàn)應用程序的定制功能??自動登陸。
  
  我們的設計思路非常簡單,在RecordStore中存儲用戶的設置和用戶的信息(用戶名和密碼),假如用戶選擇自動登陸的話,那么下次當用戶想聯(lián)網(wǎng)的時候?qū)⑻^登陸界面,系統(tǒng)會從RecordStore中讀取用戶和密碼,經(jīng)過服務器的驗證后轉(zhuǎn)入到適當?shù)慕缑妗N覍φ麄€程序進行了簡化,我們不進行聯(lián)網(wǎng),對信息的存儲也都從簡,只是為了說明RMS實現(xiàn)應用程序定制的思路,因此給出的代碼并沒有全面測試和優(yōu)化。下面是程序的截圖
  <center>[[The No.1 Picture.]]</center>
  我們用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數(shù)據(jù)的邏輯,它也非常簡單。為了簡化程序我們規(guī)定首先寫入Account然后寫入Preference,這樣我們讀取的時候只要通過recordID分別為1和2來讀取了,在實際使用的時候通常會比較復雜,我們要借助過濾器等查找,可以參考我的基于MIDP1.0實現(xiàn)個人通訊錄。
  
  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.

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 武宣县| 大新县| 康平县| 万载县| 崇左市| 黑龙江省| 新河县| 襄城县| 盐源县| 赞皇县| 磴口县| 乌兰察布市| 孟村| 陵川县| 南京市| 平阳县| 布尔津县| 普格县| 乌兰县| 连江县| 图木舒克市| 合山市| 来安县| 澎湖县| 绍兴县| 德州市| 武隆县| 新津县| 英德市| 诸暨市| 中超| 同仁县| 张家界市| 武定县| 湛江市| 金坛市| 肥城市| 蓝山县| 长兴县| 固原市| 白河县|