Java 取消并重新启动倒计时问题
嗨,我有一个倒计时功能的问题 首先我可以让它停止使用计数器倒计时; 然后我将milliUntilFinished值存储在countercur中 然后使用存储的计数器cur值重新启动计时器。 这一切都很好。但当我第二次尝试取消时,计时器就再也不会停止了。 只工作一次,我错过什么了吗?这是我的代码谢谢:Java 取消并重新启动倒计时问题,java,android,Java,Android,嗨,我有一个倒计时功能的问题 首先我可以让它停止使用计数器倒计时; 然后我将milliUntilFinished值存储在countercur中 然后使用存储的计数器cur值重新启动计时器。 这一切都很好。但当我第二次尝试取消时,计时器就再也不会停止了。 只工作一次,我错过什么了吗?这是我的代码谢谢: // Main code : MyCount counter = new MyCount(59000, 1000); counter.start(); // start ti
// Main code :
MyCount counter = new MyCount(59000, 1000);
counter.start(); // start timer at 59 seconds
///
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter.cancel(); // -- > cancelling the timer works here, the clock stops ok
//
// rest of code snipped
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MyCount counter= new MyCount(countcur,1000); // countcur is the current counter value when last cancelled
counter.start(); // This restarts timer ok. when when retrying button 1 to cancel again, it never cancels.
// rest of code snipped
// Timer Setup
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
Intent intent = new Intent();
intent.setClass(Main.this, Final.class);
startActivity(intent);
}
else {
MyCount counter = new MyCount(59000, 1000);
counter.start();
clock = counter.toString();
}
}
public void onTick(long millisUntilFinished) {
//int min = 5;
/*if (+millisUntilFinished / 1000 <= 240) {
min = 4;
mintosec = "240";
//millisUntilFinished = 59000;
}
else if (+millisUntilFinished / 1000 <= 180) {
min = 3;
mintosec = "180"; }
else if (+millisUntilFinished / 1000 <= 120) {
min = 2;
mintosec = "120"; }
else if (+millisUntilFinished / 1000 <= 60)
min =1;
mintosec = "60"; */
//TextView totmin = (TextView) findViewById(R.id.clockmin);
//totmin.setText("");
TextView totsec = (TextView) findViewById(R.id.clocksec);
totsec.setText(+duration +" : "+ millisUntilFinished / 1000);
countcur = millisUntilFinished; // store current count value for pausing and restarting
}
} // End Timer
//主代码:
MyCount计数器=新的MyCount(59000,1000);
counter.start();//在59秒时启动计时器
///
button1.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
counter.cancel();//-->如果在此处取消计时器,时钟将正常停止
//
//剩下的代码被剪掉了
button2.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
MyCount counter=new MyCount(countcur,1000);//countcur是上次取消时的当前计数器值
counter.start();//这会重新启动计时器ok。当再次尝试按钮1取消时,它不会取消。
//剩下的代码被剪掉了
//定时器设置
公共类MyCount扩展了倒计时{
公共MyCount(长百万未来,长倒计时间隔){
超级(毫秒未来,倒计时间隔);
}
公共无效onFinish(){
意图=新意图();
intent.setClass(Main.this,Final.class);
星触觉(意向);
}
否则{
MyCount计数器=新的MyCount(59000,1000);
counter.start();
时钟=计数器.toString();
}
}
公共void onTick(长毫秒未完成){
//int min=5;
/*如果在下列情况下(+millisuntilfeigned/1000,则设置的是局部变量而不是类变量:
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MyCount counter= new MyCount(countcur,1000); // countcur is the current counter value when last cancelled
将其更改为引用现有的类变量:
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter= new MyCount(countcur,1000); // countcur is the current counter value when last cancelled