I make and display an clock count down with this code
LabelField time;
long mille=0;
Timer timer=null;TimerTask task=null;
public Timerscreen() {
mille=1000*60*1;
time=new LabelField();
add(time);
timer=new Timer();
task=new TimerTask() {
public void run() {
synchronized (UiApplication.getEventLock()) {
if(mille!=0){
SimpleDateFormat date=new SimpleDateFormat("mm:ss") ;
System.out.println("================="+date.formatLocal(mille)+"====================="+Thread.activeCount());
time.setText(date.formatLocal(mille));
mille=mille-1000;
}else{
time.setText("00:00");
mille=1000*60*1;
timer.cancel();
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("Time expaired");
}
});
}
}
}
};
timer.schedule(task,0, 1000);
And when I push a new screen , I want to this clock still display and count down.
How can I do that ?
It is not possible to add a single ui
fieldormanagerinto twomanagers orscreens.. every uifieldormanagermust have at most one parent (screenormanager).So if you need a
LabelFieldwhich will hold and show time on differentscreens, then you only need to implement some sort of listener which will listen for the time changes.. and for every changes you have to update thescreenand theLabelFieldwith the new value. You have already implemented aTimerTaskwhich will provide you updated data.[Edited – added later]
you can check the following codes, not tested but something like this will solve your problem…
in the above snippet, you can implement
TimerListenerinterface in any screen instance and can get update on every time changed event by theMyTimerUtilclass. For that, you have to set an instance ofScreeA(which implementsTimerListener) viasetTimerListener()of theMyTimerUtilclass.Also need to start the timer by calling
startTimer()method.