บทที่ 6 ภาคปฏิบัติ
บทที่ 6 ภาคปฏิบัติ
ตัวอย่างที่ 1 RecordMIDlet
RecordMIDlet.java
Download Code
/* * RecordMIDlet.java * * Created on 13 ??????? 2546, 16:38 ?. */
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* An example MIDlet with simple "Hello" text and an Exit command.
* Refer to the startApp, pauseApp, and destroyApp
* methods so see how each handles the requested transition.
*
* @author sup98
* @version
*/
public class RecordMIDlet extends MIDlet{
private Preferences mPreferences;
private Form mForm;
private TextField mUserField, mPasswordField;
public void startApp() {
//Try to load the user and password from a recordstore
mPreferences = Preferences.getInstance();
if(mForm == null){
mForm = new Form("Login");
mUserField = new TextField("Name", mPreferences.getUser(), 32, 0);
mPasswordField = new TextField("Password", mPreferences.getPassword(),
32,TextField.PASSWORD);
mForm.append(mUserField);
mForm.append(mPasswordField);
mForm.addCommand(new Command("Exit", Command.EXIT, 0));
mForm.setCommandListener(new CommandListener(){
public void commandAction(Command c,Displayable s){
if(c.getCommandType() == Command.EXIT){
destroyApp(true);
notifyDestroyed();
}
}
}
);
}
Display.getDisplay(this).setCurrent(mForm);
}
/**
* Pause is a no-op since there are no background activities or
* record stores that need to be closed.
*/
public void pauseApp() {
}
/**
* Destroy must cleanup everything not handled by the garbage collector.
* In this case there is nothing to cleanup.
*/
public void destroyApp(boolean unconditional) {
//Save the user name and password
mPreferences.setUser(mUserField.getString());
mPreferences.setPassword(mPasswordField.getString());
mPreferences.store();
}
}
Preferences.java
Download Code
/* * Preferences.java * * Created on 13 ??????? 2546, 16:38 ?. */
import javax.microedition.rms.*;
/**
* An example MIDlet with simple "Hello" text and an Exit command.
* Refer to the startApp, pauseApp, and destroyApp
* methods so see how each handles the requested transition.
*
* @author sup98
* @version
*/
public class Preferences{
private static Preferences sInstance;
private String mUser, mPassword;
public static Preferences getInstance(){
if(sInstance == null)
sInstance = new Preferences("","");
RecordStore rs = null;
try{
rs = RecordStore.openRecordStore("Preferences", true);
}
catch(RecordStoreException rse){
return null;
}
String user, password;
user = password = "";
try{
byte[] userBytes = rs.getRecord(1);
byte[] passwordBytes = rs.getRecord(2);
user = new String(userBytes);
password = new String(passwordBytes);
}
catch(RecordStoreException rse){}
catch(NullPointerException npe){}
try{ rs.closeRecordStore();}
catch(RecordStoreException rse){}
sInstance.setUser(user);
sInstance.setPassword(password);
return sInstance;
}
private Preferences(String user, String password){
setUser(user);
setPassword(password);
}
public String getUser(){ return mUser;}
public String getPassword(){ return mPassword;}
public void setUser(String user){ mUser = user;}
public void setPassword(String password){ mPassword = password;}
public void store(){
RecordStore rs = null;
try{
rs = RecordStore.openRecordStore("Preferences", false);
}catch(RecordStoreException rse){ return;}
try{
byte[] userBytes = mUser.getBytes();
byte[] passwordBytes = mPassword.getBytes();
int n = rs.getNumRecords();
if(n == 0){
rs.addRecord(userBytes, 0, userBytes.length);
rs.addRecord(passwordBytes , 0, passwordBytes .length);
}
else{
rs.setRecord(1, userBytes, 0, userBytes.length);
rs.setRecord(2, passwordBytes, 0, passwordBytes.length);
}
}
catch(RecordStoreException rse){}
try{rs.closeRecordStore();}
catch(RecordStoreException rse){};
}
}
ผลการ Run
รูปที่ 50 แสดงหน้าต่าง Emulator ที่
run MIDlet ที่เขียนขึ้น
อธิบาย: MIDlet ตัวอย่างนี้ แสดงการเก็บข้อมูล ที่คุณได้ทำการกรอกเอาไว้
เครื่องครั้งก่อน เมื่อคุณทำการเปิด MIDlet ขึ้นมาอีกครั้ง ก็จะมีการดึงข้อมูลที่อยู่ใน
Record Store ขึ้นมาแสดงให้คุณได้ทราบ
ตัวอย่างที่ 2 Hotel Demo
HotelDemo.java
Download Code
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;
public class HotelDemo extends MIDlet implements CommandListener {
private Command exitCommand, backCommand, nextCommand, saveCommand;
private Display display; // The display for this MIDlet
private List menu;
private Form fr1;
private TextField tf1, tf2, tf3, tf4;
private RecordEnumeration enum;
private ByteArrayOutputStream bout = new ByteArrayOutputStream();
private DataOutputStream dout = new DataOutputStream(bout);
private RecordStore rs;
private byte [] data = null;
private ByteArrayInputStream bin;
private DataInputStream din = null;
public HotelDemo() {
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.SCREEN, 1);
nextCommand = new Command("Next", Command.BACK, 2);
saveCommand = new Command("Save", Command.BACK, 2);
backCommand = new Command("Back", Command.BACK, 2);
}
public void startApp() {
menu = new List("Hotel List", Choice.IMPLICIT);
menu.append("Add", null);
menu.append("Show All", null);
menu.addCommand(exitCommand);
menu.setCommandListener(this);
display.setCurrent(menu);
fr1 = new Form("Hotel");
fr1.append("Name");
tf1 = new TextField("", "", 15, TextField.ANY);
fr1.append(tf1);
fr1.append("Phone");
tf2 = new TextField("", "", 15, TextField.PHONENUMBER);
fr1.append(tf2);
fr1.append("Zone");
tf3 = new TextField("", "", 10, TextField.ANY);
fr1.append(tf3);
fr1.append("Price");
tf4 = new TextField("", "", 10, TextField.NUMERIC);
fr1.append(tf4);
fr1.addCommand(backCommand);
fr1.setCommandListener(this);
openRecord();
}
public void resetScreen() {
setScreen("", "", "", "");
}
public void setScreen(String name, String tel, String zone, String price) {
tf1.setString(name);
tf2.setString(tel);
tf3.setString(zone);
tf4.setString(price);
}
public void openRecord() {
try {
rs = RecordStore.openRecordStore("Hotel", true);
} catch (RecordStoreException ex) {
}
}
public void addRecord(String name, String phone, String zone, int price) {
try {
dout.writeUTF(name);
dout.writeUTF(phone);
dout.writeUTF(zone);
dout.writeInt(price);
dout.flush();
}catch (IOException ex) {
}
byte[] data = bout.toByteArray();
try {
rs.addRecord(data, 0, data.length);
} catch (RecordStoreFullException ex) {
} catch (RecordStoreException ex) {
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
try {
rs.closeRecordStore();
} catch (RecordStoreException ex) {
}
}
public void storeData() {
String name = tf1.getString();
String tel = tf2.getString();
String zone = tf3.getString();
int price = Integer.parseInt(tf4.getString());
addRecord(name, tel, zone, price);
}
public void nextRecord() {
try {
if (enum.hasNextElement()) {
int id = enum.nextRecordId();
int size = rs.getRecordSize(id);
if (data == null || data.length < size) {
data = new byte[size + 20];
bin = new ByteArrayInputStream(data);
din = new DataInputStream(bin);
}
rs.getRecord(id, data, 0);
din.reset();
String name = din.readUTF();
String tel = din.readUTF();
String zone = din.readUTF();
int price = din.readInt();
setScreen(name, tel, zone, price + "");
}
}catch (Exception ex) {
}
}
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
} else if (c == backCommand) {
fr1.removeCommand(nextCommand);
fr1.removeCommand(saveCommand);
display.setCurrent(menu);
} else if (c == saveCommand) {
storeData();
fr1.removeCommand(saveCommand);
display.setCurrent(menu);
} else if (c == nextCommand) {
nextRecord();
} else if (c == List.SELECT_COMMAND) {
switch(((List) s).getSelectedIndex()) {
case 0 :
fr1.addCommand(saveCommand);
display.setCurrent(fr1);
resetScreen();
break;
case 1 :
|