Set Timer in Blackberry Application

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();
}
}

 

 


Need more help?

Hi there, was your problem or query resolved? If not & need more assistance, please do reach out to us at info@nanostuffs.com, we'll be more than delighted to help. Nanostuffs has 7+ years of extensive Salesforce & iOS/Android experience.
Holler Box