JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

download JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

of 19

Transcript of JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

  • 7/29/2019 JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

    1/19

    J.E.D.I.

    Other Topics

    1 Objectives

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

    schedule tasks using Timers

    register incoming connections in the Push Registry

    2 Timers

    Timers and TimerTasks lets you schedule tasks to be performed at a later time. The taskcan also be scheduled to repeat at a specified interval.

    You can create a task by extending TimerTask and implementing the run() method. Therun 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 schedulethe running of the task. Each Timer runs on a separate thread. The schedule() methodhas several forms. You can set the time the task will start by specifying a delay inmilliseconds 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 willbe 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 anddiscard any scheduled task. Take note that once a Timer has been stopped, it can not be

    Mobile Application Development 1

  • 7/29/2019 JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

    2/19

    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 thespecified 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();

    Mobile Application Development 2

  • 7/29/2019 JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

    3/19

    J.E.D.I.

    TimerTask task = new CounterTask(this);

    timer.schedule(task, 2000, 1000);

    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;

    Mobile Application Development 3

  • 7/29/2019 JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

    4/19

    J.E.D.I.

    }

    public void run() {

    counter++;

    midlet.setText("" + counter);

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

    }

    }

    3 Push Functionality

    The Push Registry allows MIDlets to register inbound connections with the ApplicationManagement Software (AMS). If the program is not running, the AMS will listen forconnections on the registered inbound addresses registered by the applications. Almostall types of connections are supported, including ServerSocket and MessageConnection.

    You can register an inbound connection with the Push Registry in two ways: the staticway via the application descriptor (JAD) file or dynamically during runtime using thePushRegistry API.

    In this section we will statically register our push application in the application descriptor(JAD). Netbeans 4.1 with Mobility Pack allows us to conveniently modify the ApplicationDescriptor pertaining to the Push Registry.

    Right-click on the Project name, and click on Properties to open the Properties Page forthe project.

    Mobile Application Development 4

  • 7/29/2019 JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

    5/19

    J.E.D.I.

    Select the Push Registry branch:

    Click "Add" to register a new inbound connection:

    Mobile Application Development 5

  • 7/29/2019 JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

    6/19

    J.E.D.I.

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

    Mobile Application Development 6

  • 7/29/2019 JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

    7/19

    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 addthe javax.microedition.io.PushRegistry API in order to install our application. We shouldalso add all the API's used by our application:

    Mobile Application Development 7

  • 7/29/2019 JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

    8/19

    J.E.D.I.

    Uncheck the required radio button for all APIs:

    Mobile Application Development 8

  • 7/29/2019 JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

    9/19

    J.E.D.I.

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

    Mobile Application Development 9

  • 7/29/2019 JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

    10/19

    J.E.D.I.

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

    Mobile Application Development 10

  • 7/29/2019 JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

    11/19

    J.E.D.I.

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

    Mobile Application Development 11

  • 7/29/2019 JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

    12/19

    J.E.D.I.

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

    Mobile Application Development 12

  • 7/29/2019 JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

    13/19

    J.E.D.I.

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

    Mobile Application Development 13

  • 7/29/2019 JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

    14/19

    J.E.D.I.

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

    Mobile Application Development 14

  • 7/29/2019 JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

    15/19

    J.E.D.I.

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

    Mobile Application Development 15

  • 7/29/2019 JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

    16/19

    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);

    Mobile Application Development 16

  • 7/29/2019 JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

    17/19

    J.E.D.I.

    display.setCurrent(form);

    }

    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);

    }

    }

    Mobile Application Development 17

  • 7/29/2019 JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

    18/19

  • 7/29/2019 JEDI Course Notes-Mobile Application Devt-Lesson10-Other Topics

    19/19

    J.E.D.I.

    Use a Timer to update the time and a StringItem to display the current date and time.

    Mobile Application Development 19