我在Eclipse中为Android应用程序编写代码。我开发了一个启动画面,我需要在应用程序启动前显示5秒钟。怎么做?
答案 0 :(得分:4)
Thread timer=new Thread()
{
public void run() {
try {
sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
Intent i=new Intent(SplashScreen.this,MainActivity.class);
finish();
startActivity(i);
}
}
};
timer.start();
答案 1 :(得分:2)
像那样使用
public class SplaceScreenActivity extends Activity {
private static final int SPLASH_DISPLAY_TIME = 2500;
// SplashScreen Splash;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splacescreen);
new Handler().postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent();
intent.setClass(SplaceScreenActivity.this,
HomeScreenActivity.class);
SplaceScreenActivity.this.startActivity(intent);
SplaceScreenActivity.this.finish();
}
}, SPLASH_DISPLAY_TIME);
}
}
答案 2 :(得分:1)
为此目的使用AsyncTask或线程。
http://www.androidhive.info/2013/07/how-to-implement-android-splash-screen-2/
希望有所帮助
答案 3 :(得分:1)
Handler handler = new Handler();
Runnable run = new Runnable() {
public void run() {
// TODO Auto-generated method stub
startActivity(new Intent(SplaceActivity.this, New.class));
overridePendingTransition(0, 0);
finish();
}
};
handler.postDelayed(run, 3000);
答案 4 :(得分:1)
使用Async Class在doinbackground函数中执行sleep操作,并在post函数中执行剩下的任务
public class SplashScreenActivity extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 5000;
@Override
public void onCreate(Bundle icicle)
{ super.onCreate(icicle);
try{
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splashscreen);
}catch(Exception e){
e.printStackTrace();
}
new MyAsyncTask().execute();
}
private class MyAsyncTask extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute(){
// show your progress dialog
}
@Override
protected Void doInBackground(Void... voids){
try {
Thread.sleep(SPLASH_DISPLAY_LENGHT);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void params)
{
startActivity(new Intent(SplashScreenActivity.this, HomeActivity.class));
finish();
}
}
}
答案 5 :(得分:1)
我使用Timer
:
Timer timer = new Timer();
timer.schedule(new TimerTask(){
@Override
public void run() {
// TODO Auto-generated method stub
Intent home_page = new Intent(Splash.class,HomePage.class);
startActivity(home_page);
finish();
}}, 5000);
答案 6 :(得分:1)
尝试以下代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
final int welcomeScreenDisplay = 2000;
/** create a thread to show splash up to splash time */
Thread welcomeThread = new Thread() {
int wait = 0;
@Override
public void run() {
try {
super.run();
/**
* use while to get the splash time. Use sleep() to increase
* the wait variable for every 100L.
*/
while (wait < welcomeScreenDisplay) {
sleep(100);
wait += 100;
}
} catch (Exception e) {
} finally {
startActivity(new Intent(MainActivity.this,
HomeActivity.class));
finish();
}
}
};
welcomeThread.start();
}
}
答案 7 :(得分:0)
在splashActivity中添加以下几行代码,它将在5秒后开始您的第二个活动。
new Handler().postDelayed(new Runnable(){
public void run() {
Intent mainIntent = new Intent(splashScreen.this,MainActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
splashScreen.this.startActivity(mainIntent);
splashScreen.this.finish();
}
}, 5000);
PostDelayed 使Runnable被添加到消息队列中 在经过指定的时间后运行。可运行的意志 在这个处理程序所附加的线程上运行。
答案 8 :(得分:0)
你可以在你的Splash Activity onCreate方法中使用这样的Sleep方法:
Thread timer1 = new Thread(){
@Override
public void run(){
try{
sleep(4000);
}
catch (InterruptedException e){
e.printStackTrace();
}
finally{
Intent intent = new Intent(SplashActivity.this, NextActivity.class);
startActivity(intent);
}
}
};
timer1.start();
这需要4秒才能加载NextActivity。
答案 9 :(得分:0)
使用处理程序
public class SplashActivity extends Activity {
int secondsDelayed = 5;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
Message msg = new Message();
msg.what = 0;
mHandler.sendMessage(msg);
}
Handler mHandler = new Handler()
{
public void handleMessage(android.os.Message msg) {
switch(msg.what)
{
case 1:
{
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
break;
case 0:
{
Message ms = new Message();
ms.what = 1;
mHandler.sendMessageDelayed(ms, secondsDelayed * 1000);
}
break;
}
};
};
protected void onDestroy() {
super.onDestroy();
mHandler.removeMessages(1);
};
}
注意:Donot make Splash screen for 5 sec user will get irritated make it for 2sec