你能告诉我如何在android中使用Timer显示启动画面。我可以使用线程显示,但是线程是不错的选择,你能告诉我处理这个的最佳方法吗? 使用线程像这样
package com.example.splash_test;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
public class MainActivity extends Activity {
protected int _splashTime = 5000;
private Thread splashTread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
splashTread = new Thread() {
@Override
public void run() {
try {
synchronized(this){
wait(_splashTime);
}
} catch(InterruptedException e) {}
finally {
finish();
Intent i = new Intent();
i.setClass(MainActivity.this, SecondActivity.class);
startActivity(i);
//stop();
}
}
};
splashTread.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:2)
这个片段非常有助于设计一个计时器。试试这个......
new Timer().schedule(new TimerTask() {
@Override
public void run() {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
}, 5000);
这里我在5秒钟后开始MainActivity
。
只需将此代码放入您的启动活动的onCreate()
。
答案 1 :(得分:1)
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
public class SplashScreen extends Activity {
protected int _splashTime = 2000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
int secondsDelayed = 1;
new Handler().postDelayed(new Runnable() {
public void run() {
startActivity(new Intent(SplashScreen.this,
SecondActivity.class));
finish();
}
}, secondsDelayed * 1000);
}
}
答案 2 :(得分:0)
答案 3 :(得分:0)
public ItineraryDataEntry(final ItineraryDataEntryBuilder builder) {
this.type = builder.type;
this.field01 = builder.field01;
this.field02 = builder.field02;
this.field03 = builder.field03;
this.field04 = builder.field04;
this.field05 = builder.field05;
this.field06 = builder.field06;
this.field07 = builder.field07;
this.field08 = builder.field08;
this.field09 = builder.field09;
this.field10 = builder.field10;
this.field11 = builder.field11;
this.field12 = builder.field12;
this.field13 = builder.field13;
this.field14 = builder.field14;
this.legNumber = builder.legNumber;
this.origin = builder.origin;
this.destination = builder.destination;
this.carrier = builder.carrier;
this.fareBasisCode = builder.fareBasisCode;
this.serviceClass = builder.serviceClass;
this.flightDate = builder.flightDate;
this.departureTime = builder.departureTime;
}
...
public ItineraryDataEntryBuilder origin(final String param) {
this.origin = param;
return this;
}
public ItineraryDataEntryBuilder destination(final String param) {
this.destination = param;
return this;
}
public ItineraryDataEntryBuilder carrier(final String param) {
this.carrier = param;
return this;
}
...
答案 4 :(得分:0)
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreen.this, YourActivity.class);
startActivity(i);
// close this activity
finish();
}
}, 1000);
答案 5 :(得分:0)
Kotlin 方式
Timer().schedule(object : TimerTask() {
override fun run() {
startActivity(Intent(applicationContext, MainActivity::class.java))
}
}, 3000)