Indicate progress
You can display a field in your BlackBerry® device application that indicates that a task is proceeding. Progress is represented by a bar that fills as the task completes.
Code sample
public class ProgressIndicatorScreen extends MainScreen
{
ProgressIndicatorView view = new ProgressIndicatorView(0);
ProgressIndicatorModel model = new ProgressIndicatorModel(0, 100, 0);
ProgressIndicatorController controller = new ProgressIndicatorController();
ProgressThread _progressThread;
public ProgressIndicatorScreen()
{
setTitle("Progress Indicator Screen");
model.setController(controller);
model.addListener(new DemoProgressIndicatorListener());
view.setModel(model);
view.setController(controller);
controller.setModel(model);
controller.setView(view);
view.setLabel("Percent completion");
view.createProgressBar(Field.FIELD_HCENTER);
add(view);
MenuItem _startIndicator = new MenuItem("Start indicator", 66000, 0)
{
public void run()
{
if(_progressThread != null)
{
_progressThread.stopThread();
}
_progressThread = new ProgressThread();
_progressThread.start();
}
};
MenuItem _pauseIndicator = new MenuItem("Pause indicator", 66010, 0)
{
public void run()
{
view.getModel().cancel();
}
};
MenuItem _resumeIndicator = new MenuItem("Resume indicator", 66020, 0)
{
public void run()
{
view.getModel().resume();
}
};
addMenuItem(_startIndicator);
addMenuItem(_pauseIndicator);
addMenuItem(_resumeIndicator);
}
class ProgressThread extends Thread
{
private boolean _paused;
private boolean _stop;
public void run()
{
// Run dummy operations to simulate the processing of
// a collection of data.
for(int i = 0; i <= 100; ++i)
{
synchronized(this)
{
if(_stop)
{
break;
}
if(_paused)
{
try
{
wait();
}
catch(InterruptedException ie)
{
}
}
}
ProgressIndicatorScreen.this.model.setValue(i);
try
{
// Simulate work
sleep(250);
}
catch(InterruptedException ie)
{
}
}
}
public synchronized void setPaused(boolean paused)
{
_paused = paused;
this.notify();
}
public synchronized void stopThread()
{
_stop = true;
if(_paused)
{
// If the thread is in a paused state, wake it up
this.notify();
}
}
}
private final class DemoProgressIndicatorListener implements
ProgressIndicatorListener
{
public void cancelled()
{
_progressThread.setPaused(true);
}
public void resumed()
{
_progressThread.setPaused(false);
}
public void reset()
{
// Not implemented
}
public void setNonProgrammaticValue(int value)
{
// Not implemented
}
public void configurationChanged(Adjustment source)
{
// Not implemented
}
public void valueChanged(Adjustment source)
{
// Not implemented
}
}
}
The Progress Indicator Demo sample application that is included in the BlackBerry® Java® SDK creates and manipulates a progress bar.
Next topic: Pickers
Previous topic: Indicate activity