我正在 Eclipse 中为 Android 应用程序编写代码。我开发了一个启动画面,我需要在我的应用程序启动前显示 5 秒钟。怎么做?
问问题
12106 次
10 回答
6
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();
于 2013-10-21T10:02:11.283 回答
3
使用 Async 类在 doinbackground 函数中执行睡眠操作,并在 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();
}
}
}
于 2013-10-21T10:19:02.757 回答
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);
}
}
于 2013-10-21T09:51:10.653 回答
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);
于 2013-10-21T10:27:42.233 回答
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();
}
}
于 2013-10-21T10:29:24.260 回答
1
为此目的使用 AsyncTask 或线程。
http://www.androidhive.info/2013/07/how-to-implement-android-splash-screen-2/
希望能帮助到你
于 2013-10-21T09:47:11.250 回答
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);
于 2013-10-21T09:50:57.870 回答
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 添加到消息队列中,在经过指定的时间后运行。可运行对象将在附加此处理程序的线程上运行。
于 2013-10-21T09:47:58.233 回答
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。
于 2013-10-21T09:48:21.040 回答
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
于 2013-10-21T09:49:51.000 回答