In order to create a splash screen for a BlackBerry application, the MainScreen class must be extended and the implementation of a TrackwheelListener and KeyListener is needed such that whenever the trackwheel is clicked or the Escape key is pressed on the BlackBerry handheld, the splash screen disappears. It is also possible to set up a timer so that the splash screen only remains visible for a certain amount of time before it disappears and the application’s main screen appears. The following presents a sample implementation of a splash screen:
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();
}
}
}
In the code shown above, the SplashScreen class extends the MainScreen class. The dismiss() function is responsible for popping the SplashScreen and replacing it with the application’s main screen which is represented by the next() function.
In the constructor for SplashScreen class we set a timer that will call the dismiss() function after a period of 5000 milliseconds. So when the application starts the splash screen will appear for 5 seconds and then the main application screen will appear.
In addition to creating a Splash Screen it is necessary to create a corresponding implementation for a Trackwheel and Key Listener so that user actions such as clicking the trackwheel or pressing the escape key will cause the splash screen to disappear. Here is a sample implementation of such a listener:
import net.rim.device.api.system.*;
public 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;
}
}