MELJUN CORTES Jedi course notes mobile application devt-lesson10-other topics

download MELJUN CORTES Jedi course notes mobile application devt-lesson10-other topics

If you can't read please download the document

description

MELJUN CORTES Jedi course notes mobile application devt-lesson10-other topics

Transcript of MELJUN CORTES Jedi course notes mobile application devt-lesson10-other topics

  • 1. J.E.D.I. Other Topics1ObjectivesAfter finishing this lesson, the student should be able to: schedule tasks using Timers register incoming connections in the Push Registry2 TimersTimers 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 Timers 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 toschedule() 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 secondtimer.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 beMobile Application Development1

2. 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();Mobile Application Development 2 3. 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 Development3 4. J.E.D.I.}public void run() { counter++; midlet.setText("" + counter); System.out.println("Counter: " + counter);}}3 Push FunctionalityThe 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 5. J.E.D.I.Select the Push Registry branch:Click "Add" to register a new inbound connection:Mobile Application Development5 6. 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 Development6 7. 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 APIs used by our application:Mobile Application Development7 8. J.E.D.I.Uncheck the required radio button for all APIs:Mobile Application Development8 9. J.E.D.I.Select the "Signing" branch and check "Sign Distribution" to sign our MIDlet suite:Mobile Application Development9 10. 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 11. 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 12. 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 13. J.E.D.I.The AMS will detect the incoming connection and will ask the user for confirmation:Mobile Application Development13 14. J.E.D.I.Here is out MIDlet, launched via Push Registry (incoming SMS message):Mobile Application Development 14 15. 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 16. 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 Development16 17. 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 Development17 18. J.E.D.I.4 Exercises4.1 Time MidletCreate a MIDlet that will display the current date and time and update it every second.Mobile Application Development 18 19. J.E.D.I.Use a Timer to update the time and a StringItem to display the current date and time.Mobile Application Development19