【问题标题】:What is the preferred way for splash screen? - using handler or creating new thread?启动画面的首选方式是什么? - 使用处理程序或创建新线程?
【发布时间】:2014-11-07 15:35:55
【问题描述】:

我正在通过following this tutorial 制作启动画面。

这里,发帖人提到了两种创建启动画面的方法。

方法 1: 创建一个thread 并将时间设置为sleep,然后重定向到主应用屏幕。

  Thread background = new Thread() {
            public void run() {

                try {
                    // Thread will sleep for 5 seconds
                    sleep(5*1000);

                    // After 5 seconds redirect to another intent
                    Intent i=new Intent(getBaseContext(),FirstScreen.class);
                    startActivity(i);

                    //Remove activity
                    finish();

                } catch (Exception e) {

                }
            }
        };

        // start thread
        background.start();

方法2:设置时间为handler,调用Handler().postDelayed,设置时间后调用runnable的run方法,跳转到主应用。

 new Handler().postDelayed(new Runnable() {

        // Using handler with postDelayed called runnable run method

        @Override
        public void run() {
            Intent i = new Intent(MainSplashScreen.this, FirstScreen.class);
            startActivity(i);

            // close this activity
            finish();
        }
    }, 5*1000);

我搜索了附近的方式,找不到这两种方法之间的区别。

谁能告诉我在使用资源和内存的情况下哪个是首选方式?

【问题讨论】:

  • "闪屏的首选方式是什么?"启动画面的首选方式是不要创建一个
  • @MarcoAcierno Ya..我已经读过这篇文章cyrilmottier.com/2012/05/03/… ...但这是我的大学作业,我们将面临最有效代码的挑战...如果你知道的话请告诉我。
  • @SaDeGH_F 确定?我还阅读了这篇文章:samir-mangroliya.blogspot.in/p/…,他说处理程序是最好的方法。您能否就您的评论提供合乎逻辑的解释?
  • @SaDeGH_F 有很大的不同,永远不应该使用第一。 sleep 不是等待一段时间的正确方法。只有当您真正了解自己在做什么时,才应该使用sleep
  • @Simon 为什么你让sleep 如此可怕?我知道sleep 的真正目的不是用于现在正在使用的大多数情况,但将它用于等待不会有什么大问题。

标签: android multithreading performance android-layout splash-screen


【解决方案1】:

技术上方法 2(使用处理程序)是可行的方法。方法 1 在后台线程上调用 UI 的东西,这可能会导致不好的结果。

【讨论】:

    【解决方案2】:

    您需要启动屏幕只是为了显示徽标,还是真的需要做一些工作?

    如果您没有要实际处理的任何内容,Google 强烈建议您只加载第一个活动。

    如果你必须做一些初始化时间的工作,使用 AsyncTask 像这样:

    public class SplashActivity extends Activity {
    
      private final static int SHOW_TIME = 1300;
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
    
        new LoadTask().executeOnThreadPool();
    
      }
    
    
      class LoadTask extends AsyncTask<Void, Void, Void> {
    
        @Override
        protected Boolean doInBackground(Void... params) {
          long startTime = System.currentTimeMillis();
    
          // Do the work you need (download resources, etc...)
    
          // Stay on screen for the minimum SHOW_TIME, even if we finished before.
          long remainingTime = SHOW_TIME - (System.currentTimeMillis() - startTime);
          if (remainingTime > 0) {
            try {
              Thread.sleep(remainingTime);
            } catch (InterruptedException e) {
            }
          }
    
    
          return true;
        }
    
        @Override
        protected void onPostExecute(Boolean result) {
          // Continue to next activity.
          Intent intent = new Intent(this, YOUR_MAIN_ACTIVITY.class);
          startActivity(intent);
    
    
          finish();
    
        }
      }
    
    }
    

    【讨论】:

    • 我需要在应用程序加载时启动动画。
    • 正如我在答案中所写 - 如果它只是用于徽标,请重新考虑这是否对您的用户有好处。添加一个答案,以防您除了徽标之外不想做任何事情。
    • @Guy,嗨,我想请您注意这个问题,stackoverflow.com/questions/35539960/…,非常感谢您的帮助
    【解决方案3】:

    现在,我根据this search找到了正确答案。

    postDelayed()Runnable 放入处理程序线程的消息队列中。当控制权返回到线程的Looper 时处理消息队列。

    Thread.sleep() 只是阻塞线程。控件不返回Looper,无法处理消息。

    顺便说一句,谢谢大家。

    【讨论】:

    • “在 UI 线程中休眠几乎总是错误的”请注意,在您的第一个示例中,您休眠的是您创建的线程,而不是 UI 线程
    • @MarcoAcierno 抱歉,但我不明白。如果您对此有更好的解释,请编辑我的答案或在评论中简要写下,以便访问者获得真实信息
    • @MarcoAcierno 好的..我理解并编辑了答案...你现在能说我上面的答案是否正确吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    相关资源
    最近更新 更多