Use synchronised() and wait().
Try this code instead that displays a splash screen (/splash.png image should be placed in your resources) for 3 seconds and then resumes the midlet's operation from the splashScreenDone() method:
SplashScreenMidlet
Code:
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.io.Connector;
public class SplashScreenMidlet extends MIDlet {
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(new SplashScreen(this));
}
void splashScreenDone()
{
//Resume Code from here
}
static Image createImage(String filename)
{
Image image = null;
try
{
image = Image.createImage(filename);
}
catch (java.io.IOException ex)
{
System.out.println(ex.getMessage());ex.printStackTrace();
}
return image;
}
}
SplashScreen
Code:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class SplashScreen extends Canvas implements Runnable
{
private final SplashScreenMidlet midlet;
private Image splashImage;
private volatile boolean dismissed = false;
SplashScreen(SplashScreenMidlet midlet)
{
this.midlet = midlet;
setFullScreenMode(true);
splashImage = SplashScreenMidlet.createImage("/splash.png");
new Thread(this).start();
}
public void run()
{
synchronized(this)
{
try
{
wait(3000L); // 3 seconds
}
catch (InterruptedException e)
{
// can't happen in MIDP: no Thread.interrupt method
}
dismiss();
}
}
public void paint(Graphics g)
{
int width = getWidth();
int height = getHeight();
g.setColor(0x00FFFFFF); // white
g.fillRect(0, 0, width, height);
g.setColor(0x00FF0000); // red
g.drawRect(1, 1, width-3, height-3); // red border one pixel from edge
if (splashImage != null)
{
g.drawImage(splashImage,
width/2,
height/2,
Graphics.VCENTER | Graphics.HCENTER);
splashImage = null;
}
}
public synchronized void keyPressed(int keyCode)
{
dismiss();
}
private void dismiss()
{
if (!dismissed)
{
dismissed = true;
midlet.splashScreenDone();
}
}
}