You are on page 1of 18

J.E.D.I.

Chapter 10
Other Topics
10.1

Objectives

After finishing this lesson, the student should be able to:


schedule tasks using Timers
register incoming connections in the Push Registry

10.2

Timers

Timers and TimerTasks lets you schedule tasks to be performed at a later time. The task
can also be scheduled to repeat at a specified interval.
You can create a task by extending TimerTask and implementing the run() method. The
run method will be executed based on the schedule of the Timer.

class CounterTask extends TimerTask {


int counter = 0;
public void run() {
System.out.println("Counter: " + counter++);
}
}
To schedule a task, create a Timer and use the Timer's schedule() method to schedule the
running of the task. Each Timer runs on a separate thread. The schedule() method has
several forms. You can set the time the task will start by specifying a delay in milliseconds
or by specifying an absolute time (java.util.Date). The third parameter to schedule() is the
repeat period of the task. If the repeat period is specified, the task will be executed every
"period" milliseconds.

Timer timer = new Timer();


TimerTask task = new CounterTask();
// start the task in 8 seconds, and repeat every second
timer.schedule(task, 8000, 1000);
You can stop the timer by calling the close() method. It will stop the timer thread and
discard any scheduled task. Take note that once a Timer has been stopped, it can not be

Mobile Application Development

J.E.D.I.

restarted.

void schedule(TimerTask

task,

Long

delay)

Schedules the task for execution after the specified delay (in milliseconds).
void schedule(TimerTask

task,

Long

delay

long period)

Schedules the specified task for repeated execution, beginning after the specified
delay (in milliseconds.
void schedule(TimerTask

task,

Date

time)

Schedules the task for execution at the specified time.


void schedule(TimerTask

task,

Date

time,

long

period)

Schedules the task for repeated execution, beginning at the specified time.
void cancel()
Stops the timer, discarding any scheduled tasks.

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

import java.io.*;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Date;

public class TimerMidlet extends MIDlet implements CommandListener{


private Command exitCommand;
private Form form;
private StringItem textField;
private Display display;

public TimerMidlet() {
exitCommand = new Command("Exit", Command.EXIT, 1);
textField = new StringItem("Counter", "");

Timer timer = new Timer();


TimerTask task = new CounterTask(this);
timer.schedule(task, 2000, 1000);

Mobile Application Development

J.E.D.I.

form = new Form("Timer Test");


form.addCommand(exitCommand);
form.append(textField);
}

public void startApp() {


display = Display.getDisplay(this);
form.setCommandListener(this);
display.setCurrent(form);
}

public void pauseApp() {}


public void destroyApp(boolean unconditional) {
timer.cancel();
}

public void commandAction(Command c, Displayable d) {


if (c == exitCommand) {
destroyApp(true);
notifyDestroyed();
}
}

public void setText(String text){


textField.setText(text);
}
}

class CounterTask extends TimerTask {


int counter = 0;
TimerMidlet midlet;

public CounterTask(TimerMidlet midlet){


this.midlet = midlet;
}

public void run() {


counter++;
midlet.setText("" + counter);

Mobile Application Development

J.E.D.I.

System.out.println("Counter: " + counter);


}
}

10.3

Push Functionality

The Push Registry allows MIDlets to register inbound connections with the Application
Management Software (AMS). If the program is not running, the AMS will listen for
connections on the registered inbound addresses registered by the applications. Almost all
types of connections are supported, including ServerSocket and MessageConnection.
You can register an inbound connection with the Push Registry in two ways: the static way
via the application descriptor (JAD) file or dynamically during runtime using the
PushRegistry API.
In this section we will statically register our push application in the application descriptor
(JAD). Netbeans 6.5 with Mobility Pack allows us to conveniently modify the Application
Descriptor pertaining to the Push Registry.
Right-click on the Project name, and click on Properties to open the Properties Page for
the project.

Mobile Application Development

J.E.D.I.

Select the Push Registry branch:

Click "Add" to register a new inbound connection:

Mobile Application Development

J.E.D.I.

Repeat the previous procedure until all required inbound connections are registered. In
our case, we are listening for sms connections on port 8888 and socket connections on
port 1234:

Mobile Application Development

J.E.D.I.

Select the "API Permissions" branch:

Click "Add" to add a permission for the MIDlet suite for a particular API. We should add
the javax.microedition.io.PushRegistry API in order to install our application. We should
also add all the API's used by our application:

Mobile Application Development

J.E.D.I.

Uncheck the required radio button for all APIs:

Mobile Application Development

J.E.D.I.

Select the "Signing" branch and check "Sign Distribution" to sign our MIDlet suite:

Mobile Application Development

J.E.D.I.

Select the "Running" branch and select "Execute through OTA (Over the Air Provisioning).
This would mimic installing (and running) our application on the device.

Mobile Application Development

10

J.E.D.I.

The next step is to run our MIDlet suite. Make sure that the build run and the device
installation (via OTA provisioning) does not have an error.

Mobile Application Development

11

J.E.D.I.

To launch our MIDlets, use the WMA console (Tools -> Java Platform Manager -> J2ME
Wireless Toolkit 2.2 -> Open Utilities -> WMA: Open Console -> Send SMS...). Select the
device number, specify the port number we listed in the PushRegistry, input a message
and click "Send":

Mobile Application Development

12

J.E.D.I.

The AMS will detect the incoming connection and will ask the user for confirmation:

Mobile Application Development

13

J.E.D.I.

Here is out MIDlet, launched via Push Registry (incoming SMS message):

Mobile Application Development

14

J.E.D.I.

Here is our application launched via the Push Registry (socket on port 1234). To launch
our MIDlet this way, open a console and type "telnet localhost 1234".

Mobile Application Development

15

J.E.D.I.

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

import java.io.*;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.io.*;

public class PushMidlet extends MIDlet implements CommandListener{


private Command exitCommand;
private Form form;
private StringItem textField;
private Display display;

private String[] connections;

public PushMidlet() {
exitCommand = new Command("Exit", Command.EXIT, 1);
textField = new StringItem("Status", "");

form = new Form("Push via sms message");


form.addCommand(exitCommand);
form.append(textField);
}

public void startApp() {

connections = PushRegistry.listConnections(true);

if (connections != null && connections.length > 0){


textField.setText(
"Launched via Push Registry: " + connections[0]);
}

display = Display.getDisplay(this);
form.setCommandListener(this);

display.setCurrent(form);
}

Mobile Application Development

16

J.E.D.I.

public void pauseApp() {}


public void destroyApp(boolean unconditional) {}

public void commandAction(Command c, Displayable d) {


if (c == exitCommand) {
notifyDestroyed();
}
}

public void setText(String text){


textField.setText(text);
}
}

10.4

Exercises

10.4.1 Time Midlet

Mobile Application Development

17

J.E.D.I.

Create a MIDlet that will display the current date and time and update it every second.
Use a Timer to update the time and a StringItem to display the current date and time.

Mobile Application Development

18

You might also like