Professional Documents
Culture Documents
Chapter 10
Other Topics
10.1
Objectives
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.
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)
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 TimerMidlet() {
exitCommand = new Command("Exit", Command.EXIT, 1);
textField = new StringItem("Counter", "");
J.E.D.I.
J.E.D.I.
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.
J.E.D.I.
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:
J.E.D.I.
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:
J.E.D.I.
J.E.D.I.
Select the "Signing" branch and check "Sign Distribution" to sign our MIDlet suite:
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.
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.
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":
12
J.E.D.I.
The AMS will detect the incoming connection and will ask the user for confirmation:
13
J.E.D.I.
Here is out MIDlet, launched via Push Registry (incoming SMS message):
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".
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 PushMidlet() {
exitCommand = new Command("Exit", Command.EXIT, 1);
textField = new StringItem("Status", "");
connections = PushRegistry.listConnections(true);
display = Display.getDisplay(this);
form.setCommandListener(this);
display.setCurrent(form);
}
16
J.E.D.I.
10.4
Exercises
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.
18