1

I am trying to make my application launcha splash screen for 5 seconds while initializing various web services in the background. Here is my code:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    // Splash screen view
    setContentView(R.layout.splashscreen);

    final SplashScreen sPlashScreen = this;   

    // The thread to wait for splash screen events
    mSplashThread =  new Thread()
    {
        @Override
        public void run()
        {
            try {
                synchronized(this){
                    // Wait given period of time or exit on touch
                    wait(5000);
                }
            }
            catch(InterruptedException ex)
            {                    
            }
            finally 
            {

                finish();


                // Run next activity
                Intent intent = new Intent();
                intent.setClass(sPlashScreen, Splash_testActivity.class);
                startActivity(intent);
                stop();
            }
        }
    };

    mSplashThread.start(); 

    for (int i=0;i<100;i++)
    Log.d("splash test", "initialize web methods");
}

Now what I think should happen is that while the splash screen is displayed, the application should log "initialize web methods."

But what actually happens is that the log is added only after the slash screen disappears.

What am I doing wrong??

Tudor
  • 61,523
  • 12
  • 102
  • 142
abhinav.mehra
  • 125
  • 1
  • 1
  • 7

2 Answers2

1

Try to do it this way. This tutorial is simple and flexible. This is what you need:

// You initialize _splashTime to any value

// thread for displaying the SplashScreen
Thread splashTread = new Thread() {
    @Override
    public void run() {
        try {
            int waited = 0;
            while(waited < _splashTime)) {
                sleep(100);
                waited += 100;
            }
            }
        } catch(InterruptedException e) {
            // do nothing
        } finally {
            finish();
            startActivity(new Intent("com.droidnova.android.splashscreen.MyApp"));
            stop();
        }
    }
};
splashTread.start();

Note: This code is adopted from the above url.

iTurki
  • 16,292
  • 20
  • 87
  • 132
  • thanks you replacing the wait() with a sleep() solved the problem, although Im not sure why. Any ideas??? – abhinav.mehra Jan 24 '12 at 08:49
  • @user1166495 Glad I can help. The difference between these two method still not clear to me. I found [this great answer](http://stackoverflow.com/a/1036763/543711) But I didn't get a clear picture from it, yet ! – iTurki Jan 24 '12 at 09:19
0

Run your Thread Using Handler or AsyncTask.

Nikhil C George
  • 1,207
  • 1
  • 10
  • 14