To create a splash screen for a BlackBerry smartphone
application, the MainScreen class must
be extended, key and navigation events need to be consumed, and
a timer can be used to dismiss the screen after a certain amount
of time.
Below are two code samples showing how to implement a splash
screen for BlackBerry® Device Software 4.1 and earlier, and BlackBerry
Device Software 4.2 and later. Both of the samples are very similar.
The SplashScreen class extends the MainScreen class. The dismiss() function
is responsible for removing the splash screen and replacing it with
the application's main screen. A timer is used to call the dismiss() functions
after a period of 5000 milliseconds.
Where the samples differ is how navigation events are consumed.
In BlackBerry Device Software 4.1 and earlier, the TrackwheelListener class
is used. In BlackBerry Device Software 4.2 and later, the navigationClick(), navigationUnclick(),
and navigationMovement() functions of the Screen class
are used.
Procedure
The following code sample shows the implementation of a splash
screen.
BlackBerry Device Software 4.1 and earlier
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.i18n.*;
import net.rim.device.api.system.*;
import java.util.*;
public class SplashScreen extends MainScreen {
private MainScreen next;
private Timer timer = new Timer();
private UiApplication application;
private static final Bitmap _bitmap = Bitmap.getBitmapResource("image.png");
public SplashScreen(UiApplication ui, MainScreen next)
{
super(Field.USE_ALL_HEIGHT | Field.FIELD_LEFT);
this.application = ui;
this.next = next;
this.add(new BitmapField(_bitmap));
SplashScreenListener listener = new SplashScreenListener(this);
this.addKeyListener(listener);
this.addTrackwheelListener(listener);
timer.schedule(new CountDown(), 5000);
application.pushScreen(this);
}
public void dismiss() {
timer.cancel();
application.popScreen(this);
application.pushScreen(next);
}
private class CountDown extends TimerTask {
public void run() {
DismissThread dThread = new DismissThread();
application.invokeLater(dThread);
}
}
private class DismissThread implements Runnable {
public void run() {
dismiss();
}
}
public static class SplashScreenListener implements KeyListener,
TrackwheelListener {
private SplashScreen screen;
public SplashScreenListener(SplashScreen splash) {
screen = splash;
}
/** Invoked when the trackwheel is clicked */
public boolean trackwheelClick(int status, int time)
{
screen.dismiss();
return true;
}
/** Invoked when the trackwheel is released */
public boolean trackwheelUnclick(int status, int time)
{
return false;
}
/** Invoked when the trackwheel is rolled.*/
public boolean trackwheelRoll(int amount, int status,
int time) {
return false;
}
public boolean keyChar(char key, int status, int time)
{
//intercept the ESC key - exit the splash screen
boolean retval = false;
switch (key) {
case Characters.ESCAPE:
screen.dismiss();
retval = true;
break;
}
return retval;
}
/** Implementation of KeyListener.keyDown */
public boolean keyDown(int keycode, int time) {
return false;
}
/** Implementation of KeyListener.keyRepeat */
public boolean keyRepeat(int keycode, int time) {
return false;
}
/** Implementation of KeyListener.keyStatus */
public boolean keyStatus(int keycode, int time) {
return false;
}
/** Implementation of KeyListener.keyUp */
public boolean keyUp(int keycode, int time) {
return false;
}
}
}
BlackBerry Device Software 4.2 and later
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import java.util.*;
public class SplashScreen extends MainScreen {
private MainScreen next;
private UiApplication application;
private Timer timer = new Timer();
private static final Bitmap _bitmap = Bitmap.getBitmapResource("image.png");
public SplashScreen(UiApplication ui, MainScreen next)
{
super(Field.USE_ALL_HEIGHT | Field.FIELD_LEFT);
this.application = ui;
this.next = next;
this.add(new BitmapField(_bitmap));
SplashScreenListener listener = new SplashScreenListener(this);
this.addKeyListener(listener);
timer.schedule(new CountDown(), 5000);
application.pushScreen(this);
}
public void dismiss() {
timer.cancel();
application.popScreen(this);
application.pushScreen(next);
}
private class CountDown extends TimerTask {
public void run() {
DismissThread dThread = new DismissThread();
application.invokeLater(dThread);
}
}
private class DismissThread implements Runnable {
public void run() {
dismiss();
}
}
protected boolean navigationClick(int status, int time)
{
dismiss();
return true;
}
protected boolean navigationUnclick(int status, int time)
{
return false;
}
protected boolean navigationMovement(int dx, int dy, int status,
int time) {
return false;
}
public static class SplashScreenListener implements
KeyListener {
private SplashScreen screen;
public boolean keyChar(char key, int status, int time)
{
//intercept the ESC and MENU key - exit the splash screen
boolean retval = false;
switch (key) {
case Characters.CONTROL_MENU:
case Characters.ESCAPE:
screen.dismiss();
retval = true;
break;
}
return retval;
}
public boolean keyDown(int keycode, int time) {
return false;
}
public boolean keyRepeat(int keycode, int time) {
return false;
}
public boolean keyStatus(int keycode, int time) {
return false;
}
public boolean keyUp(int keycode, int time) {
return false;
}
public SplashScreenListener(SplashScreen splash) {
screen = splash;
}
}
}