From ModMyGPhone Wiki
The basic logic is to use a thread to do all the background work while the image is being displayed.
public class Splashscreen extends Activity
{
ImageView splash;
final Handler mHandler = new Handler();
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
splash = (ImageView) findViewById(R.id.splash);
Thread t = new Thread()
{
public void run()
{
mHandler.postDelayed(update, 4000);
}
};
t.start();
}
- mHandler is used to send the runnable when our data processing is done.
final Runnable update = new Runnable()
{
public void run()
{
splash.setVisibility(View.GONE);
}
};
}
- The update Runnable object will run when it receives the runnable from mHandler.
- The above code takes-off the image after 4 seconds. But in most conditions, you would want to remove it as soon as the processing is done. For this, replace the postDelayed message with,
mHandler.post(update);
- In this case, if your application requires less processing during startup, the splashscreen will not be visible.
Alternate Method
- In the above code we sent a Runnable object to hide the image. We can also send a message(android.os.Message).
private Handler mhandler = new Handler()
{
public void handleMessage(Message msg)
{
splash.setVisibility(View.GONE);
super.handleMessage(msg);
}
};
- In this case, we have to define mHandler and override the method handleMessage(Message). We won't need the Runnable object now.
mHandler.sendMessageDelayed(msg, 4000);
- The post/postDelayed method will be replaced by the sendMessage/sendMessageDelayed method.
So, our new source-code will be,
public class Splashscreen extends Activity
{
ImageView splash;
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
splash = (ImageView) findViewById(R.id.splash);
Thread t = new Thread()
{
public void run()
{
Message msg= new Message();
mHandler.sendMessageDelayed(msg, 4000);
}
};
t.start();
}
private Handler mHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
splash.setVisibility(View.INVISIBLE);
super.handleMessage(msg);
}
};
}
_______________
Download Source
_______________