Android:不显示启动画面,为什么?
我正在努力学习Android。从到目前为止我读过的文档中,我无法弄清楚如何显示启动视图(在睡眠期间屏幕保持空白)。看来我需要为主布局启动一个新的活动,但这似乎很浪费(飞溅应该永远消失,我想重用它的线程)。
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Ext3 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Log.v("Ext3", "starting to sleep");
try {
Thread.sleep (5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.v("Ext3", "done sleeping");
setContentView (R.layout.main);
}
}
I am working on learning Android. From the documents I have read so far I can't figure out how to get the splash View to show (during the sleep the screen stays blank). It appears I need to start a new activity for the main layout, but this seems wasteful (the splash should be forever gone, I'd like to reuse its thread).
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Ext3 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Log.v("Ext3", "starting to sleep");
try {
Thread.sleep (5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.v("Ext3", "done sleeping");
setContentView (R.layout.main);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信你的启动屏幕永远不会显示,因为你永远不会给 UI 线程(你所在的)绘制它的机会,因为你只是睡在那里什么也不做。
我建议您查看 Timer 或类似的东西,而不是 Thread.sleep安排刷新和更改视图内容;另一种方法是启动一个 AsyncTask ,您可以在更改视图之前先在其中休眠现在。
不要睡眠或以任何其他方式阻塞 UI 线程,这很糟糕......(导致 ANR)
I believe your splash screen never gets shown because you never give the UI thread (that you are in) a chance to draw it since you just sleep there doing nothing.
Instead of the Thread.sleep, I would suggest you look into a Timer or something like that to schedule the refresh and changing the content of your view; an alternative would be to start an AsyncTask where you could sleep before changing the view as you are doing now.
Do not sleep or in any other way block the UI thread, that's bad... (causes ANR)
当您像这样调用
sleep
时,就会阻塞 UI 线程。相反,将对setContentView
的第二次调用放在Runnable
中,创建一个处理程序,并使用处理程序的postDelayed
方法来运行 Runnable。像这样的东西:When you call
sleep
like that, you are blocking the UI thread. Instead, put the second call tosetContentView
in aRunnable
, create a Handler, and use the Handler'spostDelayed
method to run the Runnable. Something like this:我已经在我的应用程序中尝试过这段代码,它工作得很好。也许它会对您有所帮助。
I have tried this code in my application and its working perfectly.May be it will help you.
这是基本启动屏幕的片段
并在您的 AndroidManifest.xml 中放置
希望这对您有用:)
This is the snippet for a basic splash screen
And in your AndroidManifest.xml put
Hopefully this works for you :)