【问题标题】:Splash screen with background tasks带有后台任务的启动画面
【发布时间】:2012-01-24 07:37:05
【问题描述】:

我正在尝试让我的应用程序启动一个启动屏幕 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 方法”。

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

我做错了什么??

【问题讨论】:

    标签: java android multithreading splash-screen


    【解决方案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();
    

    注意:此代码取自上述url。

    【讨论】:

    • 感谢您用 sleep() 替换 wait() 解决了问题,尽管我不知道为什么。有什么想法???
    • @user1166495 很高兴我能提供帮助。我仍然不清楚这两种方法之间的区别。我找到了this great answer,但是我还没有从中得到清晰的图片!
    【解决方案2】:

    使用 Handler 或 AsyncTask 运行您的线程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-25
      • 2019-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多