【发布时间】: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