如何通过活动开始立即开始倒计时,并在时间到来时开始新活动?提前致谢
编辑: 活动包含一个按钮。用户应该按下它直到时间结束,否则另一个活动开始
答案 0 :(得分:2)
试试这段代码
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
//start new activity
}
}.start();
答案 1 :(得分:0)
这是您正在寻找 spalshActivity
的答案@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
try {
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
Intent i = new Intent(SplashScreen.this,
MainActivity.class);
startActivity(i);
finish();
}
};
Timer timer = new Timer();
timer.schedule(timerTask, 300);// DElay of 300 mil sec
} catch (Exception e) {
Log.e("ERROR in Splash Screen", e.toString());
}
}
答案 2 :(得分:0)
您可以通过两种方式实现这一目标
方法1:创建线程并在重定向到主应用程序屏幕后准确设置睡眠时间。 方法2:设置处理程序的时间并调用Handler()。postDelayed,在设置时间并重定向到主应用程序屏幕后,它将调用runnable的run方法。
我们可以用这种给定的方式实现我们可以为MainScreen编写代码
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
public class MainScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
// METHOD 1
/****** Create Thread that will sleep for 10 seconds *************/
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 10 seconds
sleep(10*1000);
// After 10 seconds redirect to another intent
Intent i=new Intent(getBaseContext(),AnotherScreen .class);
startActivity(i);
//Remove activity
finish();
} catch (Exception e) {
}
}
};
// start thread
background.start();
//METHOD 2
/*
new Handler().postDelayed(new Runnable() {
// Using handler with postDelayed called runnable run method
@Override
public void run() {
Intent i = new Intent(MainScreen.this, AnotherScreen.class);
startActivity(i);
// close this activity
finish();
}
}, 10*1000); // wait for 10 seconds
*/
}
@Override
protected void onDestroy() {
super.onDestroy();
}}
AnotherScreen代码可以如下
import android.app.Activity;
import android.os.Bundle;
public class AnotherScreen extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.anotherScreen);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
另一个示例访问http://www.androidhive.info/2013/07/how-to-implement-android-splash-screen-2/
https://codereview.stackexchange.com/questions/31295/review-request-android-countdowntimer-activity