我正在学习Android。从我到目前为止阅读的文档中我无法弄清楚如何使用show view来显示(在睡眠期间屏幕保持空白)。看来我需要为主布局开始一个新的活动,但这看起来很浪费(飞溅应该永远消失,我想重用它的线程)。
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);
}
}
答案 0 :(得分:7)
我相信你的启动画面永远不会被显示,因为你永远不会给你的UI线程(你所在的)有机会画它,因为你只是在那里无所事事地睡觉。
我会建议您调查Timer或类似内容来安排刷新和更改视图内容,而不是Thread.sleep;另一种方法是在你改变视图之前启动一个AsyncTask,你可以睡觉。
不要睡觉或以任何其他方式阻止UI线程,这很糟糕......(导致ANR)
答案 1 :(得分:1)
当您像这样调用sleep
时,您正在阻止UI线程。相反,将第二次调用setContentView
放入Runnable
,创建一个处理程序,并使用Handler的postDelayed
方法运行Runnable。像这样:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Runnable endSplash = new Runnable() {
@Override
public void run() {
setContentView (R.layout.main);
}
}
new Handler().postDelayed(endSplash, 5000L);
}
答案 2 :(得分:1)
我已经在我的应用程序中尝试了这个代码,并且它的工作非常完美。可能会帮助你。
public class SplashScreen extends Activity {
/**
* The thread to process splash screen events
*/
private Thread mSplashThread;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Splash screen view
setContentView(R.layout.splash);
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) {
}
finish();
// Run next activity
Intent intent = new Intent();
intent.setClass(sPlashScreen, MainActivity.class);
startActivity(intent);
stop();
}
};
mSplashThread.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
return false;
}
/**
* Processes splash screen touch events
*/
@Override
public boolean onTouchEvent(MotionEvent evt) {
if (evt.getAction() == MotionEvent.ACTION_DOWN) {
synchronized (mSplashThread) {
mSplashThread.notifyAll();
}
}
return true;
}
}
答案 3 :(得分:0)
这是基本启动画面的代码段
public class Splash extends Activity {
//private ProgressDialog pd = null;
private final int SPLASH_DISPLAY_LENGTH = 3000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);
//this.pd = ProgressDialog.show(this, "Initializing..", "Initializing Infraline...", true, false);
/* New Handler to start the InfralineTabWidget-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
/* Create an Intent that will start the InfralineTabWidget-Activity. */
Intent mainIntent = new Intent(Splash.this,InfralineTabWidget.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}
在你的AndroidManifest.xml中输入
<activity android:name=".Splash" android:theme="@android:style/Theme.NoTitleBar" android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
希望这适合你:)