Set Timer in Blackberry Application

S

This Code shows how to set Timer in your blackberry Application.

Step-1: Implement Runnable interface.And add unimplemented methods.And set this fields.

private long startTime;
private Thread updater;
private boolean isRunning= false;
private final static SimpleDateFormat timerFormat = new SimpleDateFormat(“ss.SSS”);
private final Runnable displayUpdater= new Runnable()
{
public void run()
{
displayElapsedTime(System.currentTimeMillis() – SecondScreen.this.startTime);
}
};

Step-2: Add this Method.

private void displayElapsedTime(long elapsedTime)
{
edField1.setText(”        “+timerFormat.format(new Date(elapsedTime)));
}

step-3:

public void run() {
try
{
while(isRunning)
{
UiApplication.getUiApplication().invokeAndWait(displayUpdater);
Thread.sleep(50);
}
}
catch(InterruptedException ie) {}
}

Step-4: Inside the FieldChanged Method.

To Start the timer –

if(start == field){
startTime= System.currentTimeMillis();
isRunning= true;
updater= new Thread(this);
updater.start();
}

To Stop the Timer –

if(stop == field){
if(isRunning)
{
long elapsed= System.currentTimeMillis() – startTime;
isRunning= false;
displayElapsedTime(elapsed);
Thread.yield();
currentTime = edField1.getOriginal().toString();
}
}

 

 

About the author

smita.kale
By smita.kale

Category