..
In this article we will build a small application in Java ME (Java Micro Edition) with a timer and an alarm can function as a normal alarm clock. This application should allow us in particular through the appropriate set of graphical controls the number of hours, minutes and seconds, start the timer, and at the end of the latter to start playing a media file as a ringtone.
First, I consider it important to specify a mechanism that is independent from the user interface (the MIDlet) and that allows us to easily implement this system. Then we introduce a class and an interface that will call timer alarm, able to model the behavior of the timer outside the context in which it will be used.
The alarm interface provides two basic methods, where you have to manage the implementation of the update time elapses and the alarm playback. Here is the 'interface:
public interface Alarm {
public void refresh (String time);
public void play ();
}
The Timer class is simply a thread that starts with an integer value, starts to decrease it at a later time (Thread.sleep ()).
It uses a type of alarm to notify changes in the value of the timer and the expiration of this' Most Recent call the play () method:
public class Timer extends Thread {
private Alarm alarm;
private int time;
public Timer (Alarm alarm) {
this.alarm = alarm;
}
public void set (int time) {
this.time = time;
}
public void run () {
try {
while (time> 0) {
time -;
alarm.refresh (toString ());
Thread.sleep (1000);
}
alarm.play ();
}
catch (InterruptedException ex) {}
}
public String toString () {
int sec = time;
String times = "";
Dp String = "";
int h = sec/3600;
int m = (s-(h * 3600)) / 60;
sec = sec-(m * 60) - (h * 3600);;
if (h <10) + times = "0" + h + dp;
else dp + times + = h;
if (m <10) + times = "0" + m + dp;
else dp + times + = m;
if (sec <10) + times = "0" + sec;
else s + = times;
return times;
}
}
The toString () converts an integer representing the seconds value in the string hh: mm: ss.
For example, if time is 120 then the toString () will return 00:02:00, which is two minutes.
This issue we will return shortly useful in the implementation of the graphical interface.
The user interface
Let us now see how to proceed craere a user interface that allows us to interact with the clock, glancing at the main stages in the construction has the entire application.
We will use two classes for this purpose:
| |
E-commerce with ASP (Ebook)
ECommerce and Shopping Cart with ASP. Only 35 €. |
| |
Photoshop (Course)
Web graphics and photo editing with the popular Adobe Photoshop. From 49 €. |
| |
Web Marketing (Course)
Site promotion, search engines and marketing. From 39 €. |