This package adds the Timer Service by adding two additional interfaces. The idea is the Timer Service to provide universal timers which can be used by other bundles if they need them. When the Util Bundle is started, the Timer Service is registered and started, too. To use the Timer Service, you have to get it from the Bundle Context.
To create a timer, you must invoke Timer's method notifyAfter(listener, timePeriod, event) and pass correct parameters to it.
listener is object of a class implementing the TimerListener interface whose time(event) method will be invoked after the given timePeriod (in seconds) with event specified in the notifyAfter method. The Timer Service makes an event queue from all started timer listeners and when some timePeriod of them passes, it invokes its timer method with specified event and removes it from queue. If the notifyAfter method is invoked with listener and event that already exist in queue, the previous listener is removed and the new one replaces it.
event is an int parameter. When more than one timer is started with the same listener, event shows which of them is to be notified.
With the removeLisener(listener, event) method it is
possible to remove a listener with the specified event from the
queue before its timePeriod has passed. If the listener
with the specified event is not found, nothing happens.
Here is an example which starts 3 timers with diferent timerPeriod. The timer with lisener lsn1 and event 1 will be started again with diferent timePeriod, the Timer Service will remove the previous timer and only new one will stay in the queue. The timer with lisener lsn2 and event 2 will be removed from the queue befor its timePeriod has passed. Only timer with lisener lsn1, timePeriod 5 sec and event 1 will be notified by the Timer Service when its timePeriod has passed.
|
The TestTimerListener class implements the TimerListener interface and its method timer(event) will be invoked every time a timerPeriod passes.
|