每次调用方法时,计时器运行得更快

时间:2014-01-11 19:21:04

标签: android performance methods timer call

我的Android应用程序有点问题。我试着编写一个计时器,每次按下按钮都会重启。但是如果我这样做,每次调用timer方法时定时器都会更快。我不知道,为什么这样做......

public void Dev(View view) {
    cube=(int) (Math.random()*6+1);
    TextView textView = (TextView)findViewById(R.id.textView1);
    textView.setText("Insert number: "+String.valueOf(cube));
    Button play = (Button)findViewById(R.id.button11);
    play.setVisibility(View.INVISIBLE);
    ablauf();   
}

public void ablauf() {
    //progress.cancel();
    progressStatus = 200;
    progressBar = (ProgressBar) findViewById(R.id.progressBar1);
    progressBar.setMax(200);
    progressBar.setProgress(200);

    progress=new Timer();
    tasklol= new TimerTask() {
        public void run() {
            progressmethod();
        }
    };

    progress.scheduleAtFixedRate(tasklol, 0, 30);
}


public void progressmethod() {
    progressBar.setProgress(progressStatus);
    progressStatus=progressStatus-1;

    if(progressStatus<0) {
    }
}

public void stopTimers(){

    progress.cancel();




}

以下是调用方法的Button示例:

public void B2 (View view)
{
    if (filled2==false) {
        ablauf();
        stopTimers();
        Button button2 = (Button)findViewById(R.id.button2);
        button2.setText(String.valueOf(cube));
        b=cube;
        TextView textView = (TextView)findViewById(R.id.textView1);
        cube=(int) (Math.random()*6+1);
        textView.setText("Insert number: "+String.valueOf(cube));
        filled2=true;
        counter=counter+1;

        if (counter==9) {
            Berechnen(view);
            textView.setText("Press play to start");
        } else {
            ablauf();
        }
    }
}

如果你能帮助我们pleeeeeeeeeeease,那将是非常好的;)

1 个答案:

答案 0 :(得分:0)

您可以在几个地方调用方法ablauf(),并在该方法中初始化并创建计时器和计时器任务,但不要正确停止它们。因此,您有多个计时器在运行。