Upload
others
View
9
Download
0
Embed Size (px)
Citation preview
1
Faculty of Computer Science and Information Systems
Jazan University
MOBILE COMPUTING (CNET 426)
LAB MANUAL
(MOBILE APPLICATION DEVELOPMENT LAB)
(Updated February 2017)
Revised and Updated By:
Dr SHAMS TABREZ SIDDIQUI
2
LIST OF LAB PROGRAMS
Program 1 J2ME application to display a message.
Program 2 J2ME application to display an image.
Program 3 J2ME application to display a Ticker message
Program 4 CheckBox Button in J2ME using ChoiceGroup
Program 5 Displaying Selection menu objects with form choice group with J2ME
Program 6 Radio Button Program using Choice Group for listing the objects to cut copy paste
Program 7 J2ME application to demonstrate the CalenderMIDlet.
Program 8 Displaying and inserting system date and time field objects with form choice group
with J2ME
Program 9 StringItem Program for question & answer with commands Program.
Program 10 Text Box Capturing Program
Program 11 Text Field Midlet Program
Program 12 Checking the phone number validation using Text box
Program 13 A J2ME application to retrieve the local Bluetooth device details.
Program 14 A J2ME application demonstrating on how to create a simple phone book.
3
Steps to create a J2ME Mobile application in Netbeans environment:
1. Select File → New Project
Choose Java ME in categories section and choose Mobile Application in Projects section
and click Next.
4
2. Write the appropriate Project name and Project Location.
Make sure that the create Hello MIDlet checkbox is not checked and click Next.
3. Select the default emulator Platform and Device and click Finish.
5
4. Right Click the projects name in the Projects pane and select New → MIDlet.
5. Select the appropriate MIDlet name and note that the created java MIDlet file name and
MIDlet class name are the same.
7
Program 1: A J2ME application to display a message. (Displaying message hello world with
J2ME Mobile Edition form)
Duration: Introduction and execution of program 2 Hrs
******************** Source Code************************
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class HelloWorld extends MIDlet
{
private Form form;
private Display display;
public void startApp()
{
form = new Form("Hello World");
String msg = "Hello World!!!!!!!!!";
form.append(msg);
display = Display.getDisplay(this);
display.setCurrent(form);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
}
9
Program 2: A J2ME application program to display an image (Displaying image and hello
jazan with J2ME Mobile Edition form).
Duration: Execution of program 45 minutes
******************** Source Code************************
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class ImageExample extends MIDlet
{
private Form form;
private Display display;
private Image img;
public void startApp()
{
form = new Form("Jazan University");
String msg = "Hello Jazan";
try {
img = Image.createImage("/jazan.jpg");
}
catch(Exception e)
{ System.out.println(e.getMessage());
}
form.append(msg);
form.append(img);
display = Display.getDisplay(this);
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
} }
11
Program 3: A J2ME application to display a Ticker message (Displaying Scrolling message
with J2ME Mobile Edition form).
Duration: Execution of program 45 minutes
******************** Source Code************************
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class TickerMidlet extends MIDlet
{
Display mydisplay;
Form myform;
Ticker myTicker;
public TickerMidlet()
{
mydisplay = Display.getDisplay(this);
myform = new Form("Ticker Demo App");
myTicker= new Ticker("Welcome to Jazan University");
myform.setTicker(myTicker);
}
public void startApp()
{
mydisplay.setCurrent(myform);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional) {
}
}
13
Program 4: J2ME application to demonstrate the ChoiceGroup control
Duration: Execution of program 45 minutes
********************** Source Code*************************
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ChoiceGroupMidlet extends MIDlet
{
Form form;
Display display;
ChoiceGroup cgbestmobile;
public ChoiceGroupMidlet()
{
form = new Form("Choice Group Demo Example");
cgbestmobile = new ChoiceGroup("Select the best mobile
brand",Choice.MULTIPLE);
}
public void startApp() {
cgbestmobile.append("Samsung",null);
cgbestmobile.append("Nokia",null);
cgbestmobile.append("Apple",null);
form.append(cgbestmobile);
display = Display.getDisplay(this);
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
15
Program 5 : CheckBox Button in J2ME using ChoiceGroup (Displaying Selection menu objects
with form choice group with J2ME).
Duration: Execution of program 60 minutes
******************** Source Code************************
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class CheckBoxExample extends MIDlet implements
CommandListener {
private Display display;
private Form form;
private Command exit, choose;
private ChoiceGroup technology;
private int index;
public CheckBoxExample()
{
form = new Form("Technologies");
technology = new ChoiceGroup("Select Technology Which You Know",
Choice.MULTIPLE);
exit = new Command("Exit", Command.EXIT, 1);
choose = new Command("Choose", Command.SCREEN, 2);
}
public void startApp() {
display = Display.getDisplay(this);
technology.append("JAVA", null);
technology.append("J2ME", null);
technology.append("J2EE", null);
technology.append("JSF", null);
index = form.append(technology);
16
form.addCommand(exit);
form.addCommand(choose);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){
notifyDestroyed();
}
public void commandAction(Command c, Displayable displayable){
String label = c.getLabel();
if (label.equals("Choose")) {
StringItem message[] = new StringItem[technology.size()];
boolean get[] = new boolean[technology.size()];
technology.getSelectedFlags(get);
for (int i = 0; i < get.length; i++) {
if (get[i]) { message[i] = new StringItem("Your Choice is: ",
technology.getString(i));
form.append(message[i]);
}
}
form.delete(index);
form.removeCommand(choose);
} else if (label.equals("Exit")){
destroyApp(false);
}
}
}
18
Program 6: Radio Button Program using Choice Group for listing the objects to cut copy paste
Duration: Execution of program 45 minutes
********************** Source Code*************************
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
public class CutCopyPaste extends MIDlet
implements CommandListener
{
private Form form;
private String actions[]= {"cut", "copy", "paste", "delete",
"select all", "unselect all"};
private List actionList;
public CutCopyPaste()
{
actionList = new List("Edit", List.EXCLUSIVE, actions, null);
actionList.addCommand(new Command("Select list item",
Command.SCREEN, 1));
actionList.setCommandListener(this);
}
public void startApp()
{
Display display;
display = Display.getDisplay(this);
display.setCurrent(actionList);
}
19
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
notifyDestroyed();
}
public void commandAction(Command c, Displayable d)
{
int i = actionList.getSelectedIndex();
String selectedItem = actionList.getString(i);
Alert alert = new Alert("Your Selection");
alert.setString(selectedItem);
alert.setTimeout(5000); // 5 seconds
Display display = Display.getDisplay(this);
display.setCurrent(alert, display.getCurrent() );
}
}
21
Program 7: CalenderMIDlet
Duration: Execution of program 45 minutes
******************** Source Code************************
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import java.util.Date;
import java.util.TimeZone;
public class CalendarMIDlet extends MIDlet{
private Form form;
private Display display;
private DateField calender;
private static final int DATE = 0;
public CalendarMIDlet(){
calender = new DateField("Date In:", DateField.DATE,
TimeZone.getTimeZone("GMT"));
}
public void startApp(){
display = Display.getDisplay(this);
Form form = new Form("Calendar");
form.append(calender);
display.setCurrent(form);
}
public void pauseApp(){}
public void destroyApp(boolean destroy){
notifyDestroyed();
}
}
23
Program 8: J2ME application to demonstrate the DateField control (Displaying and inserting
system date and time field objects with form choice group with J2ME Mobile Edition form)
Duration: Execution of program 45 minutes
********************** Source Code*************************
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class DateTimeMidlet extends MIDlet {
Form form;
Display display;
DateField datetime;
public DateTimeMidlet()
{
form = new Form("DateField Control Demo Example");
datetime = new DateField("Date and Time",
DateField.DATE_TIME);
}
public void startApp()
{
form.append(datetime);
display = Display.getDisplay(this);
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
25
Program 9: StringItem Program for question & answer with commands Program
Duration: Execution of program 60 minutes
******************** Source Code************************
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class StringItemExample extends MIDlet
implements CommandListener
{
private Display display;
private Form form;
private StringItem question;
private Command giveup;
private Command exit;
public StringItemExample()
{
display = Display.getDisplay(this);
question = new StringItem("Question: ","A plane crashes on the
border between Canada and the US. Where are the survivors
buried?");
giveup = new Command("Give Up", Command.SCREEN, 1);
exit = new Command("Exit", Command.EXIT, 1);
form = new Form("Quiz");
form.addCommand(exit);
form.addCommand(giveup);
form.append(question);
form.setCommandListener(this);
}
public void startApp()
{
26
display.setCurrent(form);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command command, Displayable
displayable)
{
if (command == giveup)
{
question.setLabel("Answer: ");
question.setText("Survivors are not buried.");
form.removeCommand(giveup);
}
else if (command == exit)
{
destroyApp(false);
notifyDestroyed();
}
}
}
28
Program 10: Text Box Capturing Program
Duration: Execution of program 60 minutes
******************** Source Code************************
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class TextBoxCapture extends MIDlet implements
CommandListener
{
private Display display;
private TextBox textbox;
private Command submit;
private Command exit;
public TextBoxCapture()
{
display = Display.getDisplay(this);
submit = new Command("Submit", Command.SCREEN, 1);
exit = new Command("Exit", Command.EXIT, 1);
textbox = new TextBox("First Name:", "", 30, TextField.ANY);
textbox.addCommand(exit);
textbox.addCommand(submit);
textbox.setCommandListener(this);
}
public void startApp()
{
display.setCurrent(textbox);
}
public void pauseApp()
{
}
29
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command command, Displayable
displayable)
{
if (command == submit)
{
textbox.setString("Hello, " + textbox.getString());
textbox.removeCommand(submit);
}
else if (command == exit)
{
destroyApp(false);
notifyDestroyed();
}
}
}
31
Program 11: Text Field Midlet Program
Duration: Execution of program 45 minutes
******************** Source Code************************
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class TextFieldExample extends MIDlet implements
CommandListener{
private Form form;
private Display display;
private final TextField name, company;
private final Command ok;
public TextFieldExample(){
name = new TextField("Name:", "", 30, TextField.ANY);
company = new TextField("Company Name:", "", 30, TextField.ANY);
ok = new Command("OK", Command.OK, 2);
}
public void startApp(){
display = Display.getDisplay(this);
Form form = new Form("Text Field");
form.append(name);
form.append(company);
form.addCommand(ok);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp(){
}
public void destroyApp(boolean destroy){
notifyDestroyed();
32
}
public void showInput(){
display = Display.getDisplay(this);
String n = name.getString();
String c = company.getString();
Form form = new Form("Input Value");
form.append(n);
form.append(c);
display.setCurrent(form);
}
public void commandAction(Command c, Displayable d) {
String label = c.getLabel();
if(label.equals("OK")){
showInput();
}
}
}
34
Program 12: Checking the phone number validation using Text box
Duration: Execution of program 60 minutes
******************** Source Code************************
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class VerifyPhonenumber_h4 extends MIDlet implements
CommandListener
{
private Display display;
private Form form;
private Command cmdTest;
private Command cmdExit;
private TextField phoneNum;
private String areaCodes[] = {"050", "040", "044", "041",
"0400", "058", "0590"};
public VerifyPhonenumber_h4()
{
display = Display.getDisplay(this);
cmdTest = new Command("Check", Command.SCREEN, 1);
cmdExit = new Command("Exit", Command.EXIT, 1);
phoneNum = new TextField("Phone:", "", 11,
TextField.PHONENUMBER);
form = new Form("Insert the phone number");
form.addCommand(cmdExit);
form.addCommand(cmdTest);
form.append(phoneNum);
form.setCommandListener(this);
}
public void startApp()
{
35
display.setCurrent(form);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
public void commandAction(Command c, Displayable s)
{
if (c == cmdTest)
{
if (phoneNum.size() >= 9 && phoneNum.size() <= 11)
{
char buf[] = new char[11];
phoneNum.getChars(buf);
// Call a method to check the area code table.
String checkStr = checkCode(buf) ? "is ok" : "is not ok";
StringItem tmp = new StringItem(null, "Area code " + checkStr );
if (form.size() == 1)
form.append(tmp);
else
form.set(1, tmp);
}
}
else if (c == cmdExit) {
destroyApp(false);
notifyDestroyed();
}
}
private boolean checkCode(char [] buf) {
String str = new String(buf, 0, 3);
String str2 = new String(buf, 0, 4);
for (int x = 0; x < areaCodes.length; x++) {
// If we find a match in the table
36
if (str.equals(areaCodes[x])) return true;
if (str2.equals(areaCodes[x])) return true;
}
return false;
}
}
OUTPUT:
37
Program 13: A J2ME application to retrieve the local Bluetooth device details.
Duration: Execution of program 60 minutes
******************** Source Code************************
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.bluetooth.*;
public class LocalBTMidlet extends MIDlet implements
CommandListener
{
Display display;
Form form;
Command exit;
LocalDevice local = null;
public void LocalBTMidlet()
{
}
public void startApp()
{
form = new Form("Local Bluetooth Details");
exit = new Command("Exit",Command.EXIT,1);
form.addCommand(exit);
form.setCommandListener(this);
display = Display.getDisplay(this);
try
{
local = LocalDevice.getLocalDevice();
String address = local.getBluetoothAddress();
String name = local.getFriendlyName();
38
form.append("Address: "+address+"\n");
form.append("Name: "+name+"\n");
}
catch(Exception e)
{
form.append("Error: "+e+"\n");
}
display.setCurrent(form);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command cmd, Displayable d)
{
if( cmd == exit )
{
this.destroyApp(true);
this.notifyDestroyed();
}
}
}
40
Program 14: A J2ME application demonstrating on how to create a simple phone book
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class phonebook extends MIDlet implements CommandListener
{
private Command exit, next, New;
private TextBox name, number;
private Display display;
private String nam, no;
private Form form;
public phonebook()
{
next = new Command("Next", Command.SCREEN, 2);
exit = new Command("Exit", Command.SCREEN, 2);
New = new Command("New", Command.SCREEN, 2);
}
public void startApp()
{
display = Display.getDisplay(this);
name = new TextBox("Enter Name", "", 30, TextField.ANY);
name.addCommand(next);
name.setCommandListener(this);
number = new TextBox("Enter Number", "", 13,
TextField.PHONENUMBER);
number.addCommand(New);
number.addCommand(exit);
number.setCommandListener(this);
display.setCurrent(name);
}
41
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
notifyDestroyed();
}
public void commandAction(Command c, Displayable s)
{
String label = c.getLabel();
if (label.equals("Exit"))
{
nam = name.getString();
no = number.getString();
System.out.println("Name = " + name.getString() + ",Number =
"+ number.getString());
destroyApp(false);
}
else if (label.equals("Next"))
{
number.setString("");
display.setCurrent(number);
}
else if (label.equals("New"))
{
display.setCurrent(name);
nam = name.getString();
no = number.getString();