android重置计时器多次

时间:2015-07-06 18:51:19

标签: java timer countdowntimer

我的第一个问题! 我正在尝试创建一个应用程序,您可以随机生成一段时间(1秒到5分钟之间)并开始倒数。通过按下屏幕上的按钮,计时器将重置为参数之间的新随机时间,一遍又一遍。

我的问题是它正在使用countDownTimer类中的方法.cancel(),它只停止它。因此,当我生成一个新的随机时间并使用.start()方法时,它会启动倒计时和我不想要的旧计时器。

package com.example.kasutaja.refugecamp;

import android.os.CountDownTimer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;


public class MainActivity extends ActionBarActivity {

Button Start;
TextView textViewTime;
TextView textViewScore;
public boolean run=true;
public long milliss;
public long aeg;
public long score;
public int a;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Start=(Button)findViewById(R.id.Start );
    textViewTime=(TextView)findViewById(R.id.textViewTime);
    textViewScore=(TextView)findViewById(R.id.textViewScore);
    textViewTime.setText("00:00:00");
    aeg=Randoom();
    final CounterClass timer= new CounterClass(aeg,1000);
    Start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            if (run) {

                System.out.println(aeg + " aeg");
                aeg = aeg - milliss;
                System.out.println(aeg + " aeg2");
                score = score + aeg;
                System.out.println(milliss + " milliss");
                System.out.println(score + " score");
                String hmse = String.format("%02d:%02d:%02d",  TimeUnit.MILLISECONDS.toHours(score),
                        TimeUnit.MILLISECONDS.toMinutes(score) -  TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(score)),
                        TimeUnit.MILLISECONDS.toSeconds(score) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(score)));

                textViewScore.setText(hmse);
                timer.cancel();
                aeg = Randoom();
                final CounterClass timer = new CounterClass(aeg, 1000);
                timer.start();

            }


        }

    });

    timer.start();

}

public long Randoom() {
    Random rand = new Random();
    long aeg = rand.nextInt(300000 - 5000) + 5000;
    return aeg;
}
public class CounterClass extends CountDownTimer{

    public CounterClass(long millisInFuture,long countDownInterval){
        super(millisInFuture,countDownInterval);
    }
    @Override
    public void onTick(long millisUntilFinished){

        long millis =millisUntilFinished;

        String hms=String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
                TimeUnit.MILLISECONDS.toMinutes(millis)- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                TimeUnit.MILLISECONDS.toSeconds(millis)-TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));

        textViewTime.setText(hms);
        milliss=millis;
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

1 个答案:

答案 0 :(得分:0)

找到解决方案。

只需重新初始化

final CounterClass timer = new CounterClass(aeg, 1000);
onClick方法中的

this.timer = new CounterClass(aeg, 1000);