闪屏与后台任务 [英] Splash screen with background tasks

查看:100
本文介绍了闪屏与后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的应用程序launcha启动画面5秒钟,而在后台初始化各种网络服务。这里是我的code:

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.

我在做什么错了?

推荐答案

尝试去做的这样。本教程是简单,灵活。这就是你所需要的:

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

注意:此code从上面的URL采用

Note: This code is adopted from the above url.

这篇关于闪屏与后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆