Advanced Programming

Record Store

RecordStore is a class that provides data storing
function in memory. RecordStore contains a set of records that can be accessed
by some different MIDlet applications. Data stored in RecordStore, will not be
lost even if the mobile device is rebooting, turned off, or hang. Data stored
in RecordStore, can be shared using another MIDlet if the privilege is given.
The set of records that packaged in a RecordStore will be called with unique.
Every record has RecordID as the primary key of the record. We can access the
data in RecoredStore by direct accessing based on the RecordID or using the
record enumeration.

MyRecordStore.java

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import javax.microedition.rms.*;

import java.util.*;

public class MyRecordStore extends MIDlet implements
CommandListener{

private Display
display = null;

private
RecordStore rs = null;

private List list
= null;

private Command
exit = new Command(“EXIT”,Command.EXIT,1);

private Command
add = new Command(“Add”,Command.ITEM,1);

private Command
delete = new Command(“Delete”,Command.ITEM,2);

private Command ok
= new Command(“OK”,Command.OK,1);

private Command
cancel = new Command(“CANCEL”,Command.CANCEL,1);

private TextBox
text = null;

private Vector
vect = null;

public
MyRecordStore()

{

try

{

rs =
RecordStore.openRecordStore(“myrecord”,
true,RecordStore.AUTHMODE_ANY,true);

vect = new Vector();

list = new
List(“Record List”,List.IMPLICIT);

text = new
TextBox(“Record”,”",20,TextField.ANY);

}

catch(Exception e)

{

e.printStackTrace();

}

}

public void startApp() {

if(display == null)

{

display = Display.getDisplay(this);

previewRecordList();

}

}

private void previewRecordList()

{

list.deleteAll();

vect.removeAllElements();

try

{

RecordEnumeration re =
rs.enumerateRecords(null,null,true);

while(re.hasNextElement())

{

Integer objInt = new
Integer(re.nextRecordId());

vect.addElement(objInt);

list.append(new
String(rs.getRecord(objInt.intValue())),null);

}

}

catch(Exception e)

{

e.printStackTrace();

}

list.addCommand(exit);

list.addCommand(add);

list.addCommand(delete);

list.setCommandListener(this);

display.setCurrent(list);

}

private void setAddRecord()

{

text.setString(“”);

text.addCommand(cancel);

text.addCommand(ok);

text.setCommandListener(this);

display.setCurrent(text);

}

public void pauseApp() {

}

public void destroyApp(boolean
unconditional) {

}

private void addData()

{

try

{

vect.addElement(new
Integer(rs.getNextRecordID()));


rs.addRecord(text.getString().getBytes(),0,text.getString().length());

}

catch(Exception e)

{

e.printStackTrace();

}

}

private void deleteData()

{

Integer recordId =
(Integer)vect.elementAt(list.getSelectedIndex());

try

{


rs.deleteRecord(recordId.intValue());

}

catch(Exception e)

{

e.printStackTrace();

}

}

public void commandAction(Command
c,Displayable d)

{

if(c == exit)

{

notifyDestroyed();

destroyApp(true);

}

else if(c == add)

{

setAddRecord();

}

else if(c == ok)

{

addData();

previewRecordList();

}

else if(c == cancel)

{

previewRecordList();

}

else if(c == delete)

{

deleteData();

previewRecordList();

}

}

}

HTTP Connection

This HTTP Connection can be used to take output
of a website page. If a website page is accessed using this connection, then we
could get the HTML tags of the website page. To create this HTTP Connection, we
need the HttpConnection interface from Connector Class that comes from
javax.microedition.io package. You can see the example of HTTP Connection
below.

MyHttp.java

import
javax.microedition.midlet.*;

import
javax.microedition.lcdui.*;

import
javax.microedition.io.*;

import
java.io.*;

public class
MyHttp extends MIDlet implements CommandListener,Runnable

{

private Display display = null;

private Form form = null;

private Command exit = new
Command(“Exit”,Command.EXIT,1);

private Command go = new
Command(“Go”,Command.OK,1);

private TextField urlTextField = null;

public MyHttp()

{

form = new Form(“Koneksi
Http”);

urlTextField = new
TextField(“URL”,”",50,TextField.URL);

form.append(urlTextField);

form.addCommand(exit);

form.addCommand(go);

form.setCommandListener(this);

}

public void startApp() {

if(display==null)

{

display = Display.getDisplay(this);

display.setCurrent(form);

}

}

public void run()

{

HttpConnection
c = null;

InputStream is
= null;

OutputStream
os = null;

int rc;

String result
= “”;

try

{

c =
(HttpConnection)Connector.open(urlTextField.getString());

c.setRequestMethod(HttpConnection.GET);


c.setRequestProperty(“User-Agent”, “Profile/MIDP-2.0
Configuration/CLDC-1.0″);


c.setRequestProperty(“Content-Languange”, “en-US”);

rc = c.getResponseCode();

if(rc!=HttpConnection.HTTP_OK)

{

System.out.println(“not
ok”);

}

is = c.openInputStream();

int len = (int)c.getLength();

if(len > 0)

{

int actual = 0;

int bytesread = 0;

byte[] data = new byte[len];

String result_temp = null;

while((bytesread!=len)

&& (actual != -1))

{

actual =
is.read(data,bytesread,len-bytesread);

bytesread += actual;

result_temp = new
String(data);

result += result_temp;

}

}

else

{

int ch;

StringBuffer sb = new
StringBuffer();

while((ch = is.read())!=-1)

{

sb.append((char)ch);

}

result = sb.toString();

}

if(is != null)

is.close();

if(os != null)

os.close();

if(c != null)

c.close();

form.deleteAll();

form.append(result);

}

catch(Exception e)

{

e.printStackTrace();

}

}

public void pauseApp() {

}

public void destroyApp(boolean
unconditional) {

}

public void commandAction(Command
c,Displayable d)

{

if(c == exit)

{

notifyDestroyed();

destroyApp(true);

}

else if(c == go)

{

Thread thread = new Thread(this);

thread.start();

}

}

}

API Programming

Mobile Media API (MMAPI)

Mobile Media API
(MMAPI) is Advanced Programming Interface that provided by J2ME to handle some
multimedia format files. This API contains classes that can be used to access
and control the multimedia files. Some file-type that can be accessed by this
API are: wave audio type, AU audio, MP3 audio with mpeg format, MIDI file,
audio tone, and video file with mpeg format. This API can access files which
are stored in local storage or streamed them from a server. The code below is
the example of J2ME application that can play a video file with mpeg format.

MyVideo.java

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import javax.microedition.media.*;

import javax.microedition.media.control.*;

import java.io.*;

public class MyVideo extends MIDlet implements
CommandListener ,Runnable, PlayerListener {

private Display
display = null;

private Form form
= null;

private Command
play = null,exit = null;

private Player
player = null;

private
VideoControl vc = null;

private Item
videoItem = null;

public MyVideo()

{

form = new
Form(“My Video Player”);

play = new
Command(“Play”,Command.OK,1);

exit = new
Command(“Exit”,Command.EXIT,1);


form.addCommand(play);


form.setCommandListener(this);

}

public void startApp() {

if(display == null)

{

display = Display.getDisplay(this);

display.setCurrent(form);

}

}

public void pauseApp() {

}

public void destroyApp(boolean
unconditional) {

System.gc();

}

public void playerUpdate(Player
player,String event,Object eventData)

{

if(event.equals(END_OF_MEDIA))

{

player.close();

form.removeCommand(play);

form.addCommand(exit);

}

}

public void run()

{

try

{


form.removeCommand(play);


InputStream is =
getClass().getResourceAsStream(“/video/phantom.mpg”);

player =
Manager.createPlayer(is,”video/mpeg”);


player.addPlayerListener(this);

player.realize();

vc =
(VideoControl)player.getControl(“VideoControl”);

videoItem
= (Item)vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE,null);


form.append(videoItem);


player.prefetch();

player.start();

}


catch(Exception e)

{


e.printStackTrace();

}

}

public void commandAction(Command
c,Displayable d)

{

if(c == exit)

{

notifyDestroyed();

destroyApp(true);

}

else if(c == play)

{

Thread thread = new Thread(this);

thread.start();

}

}

}

Description

public class MyVideo extends MIDlet implements
CommandListener ,Runnable, PlayerListener {

Here, we implement the CommandListener interface because we will use
some buttons to control this application, the Runnable interface because we
will use thread, and PlayerListener interface to handle the state change or
event that happens to the Player class.

public void playerUpdate(Player
player,String event,Object eventData)

{

if(event.equals(END_OF_MEDIA))

{

player.close();

form.removeCommand(play);

form.addCommand(exit);

}

}

The playerUpdate method must be overridden because the usage of
PlayerListener interface. This method contains the definition of what will be
happened if player is on END_OF_MEDIA event or state. In the last media, there
will be state change from player object to close state, the play command will
be omitted and the exit command will be added.

InputStream is =
getClass().getResourceAsStream(“/video/phantom.mpg”);

player = Manager.createPlayer(is,”video/mpeg”);

player.addPlayerListener(this);

The script above is used to read the mpeg format video file from video
directory, and then create the player object based on the video file that has
been read and has the video/mpeg content-type. Then, add the PlayerListener to
the player object.


player.realize();

vc =
(VideoControl)player.getControl(“VideoControl”);

videoItem
= (Item)vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE,null);


form.append(videoItem);


player.prefetch();

player.start();

From the script above, the player object will enter the realize state,
a state where the object collects information about the media that will be
read. Create the control object from VideoControl class, and then specify which
display mode that will be used based on USE_GUI_PRIMITIVE constant. Set the
control item into the form, and change the player state to be pre-fetch, where
in this state, the player object will count how long the time of video playing
and fill the buffer memory with the media data. After that, player is ready to
be played by changing its object state to be start state.

image002

The picture above is a screenshot of MyVideo project structure (this
explains the URL of “phantom.mpg” file).

Originally written by Krisna Rengga Buana.

BERITA BINUS : BINUS UNIVERSITY MENDATANGKAN DIPLOMAT HANDAL