有背景任务的飞溅屏幕

时间:2012-01-24 07:37:05

标签: java android multithreading splash-screen

我正在尝试使应用程序启动启动屏幕5秒钟,同时在后台初始化各种Web服务。这是我的代码:

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");
}

现在我认为应该发生的是,当显示启动画面时,应用程序应该记录“初始化Web方法。”

但实际发生的是,只有在斜杠屏幕消失后才会添加日志。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

尝试this way。本教程简单灵活。这就是你需要的:

// 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();

注意:此代码摘自上面的网址。

答案 1 :(得分:0)

使用Handler或AsyncTask运行线程。