J.E.D.I.                                 Other Topics1      ObjectivesAfter finishing this lesson, the student should be a...
J.E.D.I.restarted.  void schedule(TimerTask task, Long delay)           Schedules the task for execution after the specifi...
J.E.D.I.           TimerTask task = new CounterTask(this);           timer.schedule(task, 2000, 1000);           form = ne...
J.E.D.I.      }      public void run() {           counter++;           midlet.setText("" + counter);           System.out...
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. Inour case, we are listening ...
J.E.D.I.Select the "API Permissions" branch:Click "Add" to add a permission for the MIDlet suite for a particular API. We ...
J.E.D.I.Uncheck the required radio button for all APIs:Mobile Application Development                    8
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 AirProvisioning). This would mimic installin...
J.E.D.I.The next step is to run our MIDlet suite. Make sure that the build run and the deviceinstallation (via OTA provisi...
J.E.D.I.To launch our MIDlets, use the WMA console (Tools -> Java Platform Manager -> J2MEWireless Toolkit 2.2 -> Open Uti...
J.E.D.I.The AMS will detect the incoming connection and will ask the user for confirmation:Mobile Application Development ...
J.E.D.I.Here is out MIDlet, launched via Push Registry (incoming SMS message):Mobile Application Development              ...
J.E.D.I.Here is our application launched via the Push Registry (socket on port 1234). To launchour MIDlet this way, open a...
J.E.D.I.import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import java.io.*;import java.util.Timer;impor...
J.E.D.I.           display.setCurrent(form);      }      public void pauseApp() {}      public void destroyApp(boolean unc...
J.E.D.I.4 Exercises4.1 Time MidletCreate a MIDlet that will display the current date and time and update it every second.M...
J.E.D.I.Use a Timer to update the time and a StringItem to display the current date and time.Mobile Application Developmen...
Upcoming SlideShare
Loading in...5
×

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

217

Published on

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

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
217
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

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

  1. 1. J.E.D.I. Other Topics1 ObjectivesAfter 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 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 beMobile Application Development 1
  2. 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. 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 Development 3
  4. 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. 5. J.E.D.I.Select the Push Registry branch:Click "Add" to register a new inbound connection:Mobile Application Development 5
  6. 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 Development 6
  7. 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 Development 7
  8. 8. J.E.D.I.Uncheck the required radio button for all APIs:Mobile Application Development 8
  9. 9. J.E.D.I.Select the "Signing" branch and check "Sign Distribution" to sign our MIDlet suite:Mobile Application Development 9
  10. 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. 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. 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. 13. J.E.D.I.The AMS will detect the incoming connection and will ask the user for confirmation:Mobile Application Development 13
  14. 14. J.E.D.I.Here is out MIDlet, launched via Push Registry (incoming SMS message):Mobile Application Development 14
  15. 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. 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 Development 16
  17. 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 Development 17
  18. 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. 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

×