如何启动一个定时器完成后的活动? [英] How to start an activity upon the completion of a timer?
本文介绍了如何启动一个定时器完成后的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想开始新的活动SMS.java,如果我不给内30secs我的定时器回应。后30secs,新ativity应启动。谁能帮助我?第5行的类定时器扩展了CountDownTimer ..
这里的code:
// TimerAct.java
公共类TimerAct扩展活动
{
静态的TextView timeDisplay;
定时器T;
INT长度= 30000; / **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.time); timeDisplay =(的TextView)findViewById(R.id.timer);
timeDisplay.setText(剩余时间的长度+ / 1000);
T =新的Timer(长,1000);
t.start();
查看B1 = findViewById(R.id.abort);
b1.setOnClickListener(新View.OnClickListener()
{
公共无效的onClick(视图v)
{
t.cancel();
完();
}
});
}
}//Timer.java
公共类定时器扩展CountDownTimer
{
公共定时器(长millisInFuture,长countDownInterval)
{
超(millisInFuture,countDownInterval);
} 公共无效onTick(长millisUntilFinished)
{
TimerAct.timeDisplay.setText(剩余时间+ millisUntilFinished / 1000);
} 公共无效onFinish()
{
TimerAct.timeDisplay.setText(时间超过!!!);
}
}
解决方案
我要做的就是叫我CountDownTimer类从活动的方法,如:
// Timer类我里面的活动
公共类飞溅扩展CountDownTimer { 公共飞溅(长millisInFuture,长countDownInterval){
超(millisInFuture,countDownInterval);
} @覆盖
公共无效onFinish(){
nextActivity(Login.class,真);
} @覆盖
公共无效onTick(长millisUntilFinished){}
}//我的活动类方法 保护无效nextActivity(类<> MyClass的,布尔完成){
意图myIntent =新意图(这一点,MyClass的);
myIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(myIntent); 如果(完成)
完();
}
I'm trying to start a new activity "SMS.java", if I dont respond to my timer within 30secs. After 30secs, the new ativity should be started. Can anyone help me out??? The class Timer on line 5 extends a CountDownTimer.. Here's the code:
//TimerAct.java
public class TimerAct extends Activity
{
static TextView timeDisplay;
Timer t;
int length = 30000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.time);
timeDisplay = (TextView) findViewById(R.id.timer);
timeDisplay.setText("Time left: " + length / 1000);
t = new Timer(length, 1000);
t.start();
View b1 = findViewById(R.id.abort);
b1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
t.cancel();
finish();
}
});
}
}
//Timer.java
public class Timer extends CountDownTimer
{
public Timer(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
public void onTick(long millisUntilFinished)
{
TimerAct.timeDisplay.setText("Time left: " + millisUntilFinished / 1000);
}
public void onFinish()
{
TimerAct.timeDisplay.setText("Time over!!!");
}
}
解决方案
What I do is call a method from the Activity on my CountDownTimer class, like this:
//Timer Class inside my Activity
public class Splash extends CountDownTimer{
public Splash(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
nextActivity(Login.class, true);
}
@Override
public void onTick(long millisUntilFinished) {}
}
//Method on my Activity Class
protected void nextActivity(Class<?> myClass, boolean finish) {
Intent myIntent = new Intent(this, myClass);
myIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(myIntent);
if(finish)
finish();
}
这篇关于如何启动一个定时器完成后的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文