onFinish
方法所以,我有一个倒数计时器。它从20秒倒计时,onFinish()
我有意图。我还会定期根据millisUntilFinished
设置textView的文本。 我注意到,在textView显示1 second left
之后,意图在3秒后开始。
但是,如果我通过使用来自onFinish
方法的 OUTSIDE的意图切换活动,则下一个活动会很快启动。所以,
onFinish
方法开始一个意图需要比平时更长的时间?根据我对计时器的小测试,我认为我需要一种更好,更快的方式来启动我的意图,因为很明显,onFinish方法会在计时器实际启动后经过更长时间后启动。那么,我该怎么做才能更快地开始我的意图呢?我需要它立即......
public void startTimer() {
timer = new CountDownTimer(20000, 1000) {
public void onTick(long millisUntilFinished) {
int seconds = (int) millisUntilFinished/1000;
timetext.setText(seconds + ":00");
}
public void onFinish() {
Intent intent = new Intent(MainActivity.this, GameOver.class);
intent.putExtra("score", score); // pass your values and retrieve them in the other Activity using keyName
intent.putExtra("classname", "com.example.ruchir.swapproperties.MainActivity");
startActivity(intent);
}
}.start();
}
谢谢,
Ruchir
答案 0 :(得分:0)
尝试将startActivity(intent)
置于计时器之外:
public void startTimer() {
timer = new CountDownTimer(20000, 1000) {
public void onTick(long millisUntilFinished) {
int seconds = (int) millisUntilFinished/1000;
timetext.setText(seconds + ":00");
}
public void onFinish() {
Intent intent = new Intent(MainActivity.this, GameOver.class);
intent.putExtra("score", score); // pass your values and retrieve them in the other Activity using keyName
intent.putExtra("classname", "com.example.ruchir.swapproperties.MainActivity");
}
}.start();
startActivity(intent);
}
答案 1 :(得分:0)
你可以尝试一下:
public void onFinish() {
cancel(); //Cancel the countdown
Intent intent = new Intent(MainActivity.this, GameOver.class);
intent.putExtra("score", score); // pass your values and retrieve them in the other Activity using keyName
intent.putExtra("classname", "com.example.ruchir.swapproperties.MainActivity");
startActivity(intent);
}
`