安卓:定时器onTick问题 [英] Android: Timer onTick problem

查看:177
本文介绍了安卓:定时器onTick问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我的工作我的项目之一,那么我遇到过这个问题,以及我不知道这是否是我想和大家分享,希望得到一些答案来开导我一天中的错误或东西。因此,这里的问题:

我做了一个倒数计时器为我的项目,该项目将运行2分钟(这将是120秒)1000的间隔时间(1000毫秒= 1秒),那么我设置了检查,120的整数(全局声明),将每次由计时器滴答的时间减少。该整数将显示在一个TextView倒计时。当整数到达定时器的onFinish上将显示一条信息。这个消息会给我多少时间,由120减去声明整数度过的。

这里的问题是,它达到1因为它不会被最后蜱扣除之前定时器停止。最难的是,有时它有4个甚至5停在剩余的时间。谁能帮助我这一个?在此先感谢!

下面是我的code做进一步的了解:

  //声明定时器
INT定时器= 120;

//启动定时器
gameTimer =新gameTimer(120000,1000);
    gameTimer.start();

私有类gameTimer扩展CountDownTimer {
公共gameTimer(长的startTime,长间隔)
{
    超(startTime时,间隔);
}

公共无效onTick(长millisUntilFinished)
{
   //对蜱扣除定时器1
  计时器 -  = 1;
   TextView的定时器1 =(TextView中)findViewById(R.id.textView1);
   timer1.setText(+定时器);
}

公共无效onFinish()
{
    //执行结束动作在完成THE GAME
    结束时间= 120  - 定时器;
    TextView的endtime1 =(TextView中)findViewById(R.id.textView2);
    1.setText(+结束时间);
}
}
 

解决方案

第一 *的:这是你的类扩展的* CountDownTimer

 公共类GameTimer扩展CountDownTimer {

私人YourActivity环境;
私人诠释定时器= 120;
私人诠释结束时间;
私人TextView的定时器,endTime1;

公共gameTimer(YourActivity背景下,长的startTime,长间隔)
{
    //通过你的活动范围
    this.context =背景;

    //获取TextView的显示结果
    定时器1 =(TextView中)this.context.findViewById(R.id.textView1);
    endTime1 =(TextView中)this.context.findViewById(R.id.textView2);

    超(startTime时,间隔);
}

公共无效onTick(长millisUntilFinished)
{
   //对蜱扣除定时器1
  计时器 -  = 1;

  //覆盖的方法runOnUIThread
  context.runOnUiThread(新的Runnable(){

      @覆盖
      公共无效的run(){
         timer1.setText(+定时器);
      }
  });

}

公共无效onFinish()
{
    //执行结束动作在完成THE GAME
    结束时间= 120  - 定时器;

     //覆盖的方法runOnUIThread
  context.runOnUiThread(新的Runnable(){

      @覆盖
      公共无效的run(){
         endTime1.setText(+结束时间);
      }
  });

}
}
 

二:在你的活动(在方法 的onCreate() ,实例化你的 GameTimer 并启动:

  //实例化的GameTimer和上下文传递给它
GameTimer gameTimer =新GameTimer(本,120000,1000);
    gameTimer.start();
 

Hi I'm working on one of my project then I encountered this problem, well I don't know if it is a bug or something I just want to share hoping to get some answer to enlighten my day. So here's the problem:

I made a countdown timer for my project which will run for 2 minutes (that will be 120 seconds) 1000 is the interval time (1000ms = 1s) then I set a checker, an integer of 120 (declared globally) which will be diminished by one each time the timer ticks. The integer will be shown on a textView counting down. then will show a message when the integer reaches the onFinish of the timer. That message will give how much time I've spent by deducting the declared integer by 120.

The problem here is that the timer stops before it reaches 1 given that it will not be deducted by the last tick. The hardest part is that sometimes it stops with 4 or even 5 on the remaining time. Can Anyone help me about this one? Thanks in advance!

Here's my code for further understanding:

//declared a timer
int timer = 120;

//starts the timer
gameTimer = new gameTimer(120000, 1000);
    gameTimer.start();

private class gameTimer extends CountDownTimer{
public gameTimer(long startTime, long interval)
{
    super(startTime, interval);
}

public void onTick(long millisUntilFinished)
{
   //on tick deduct timer by 1
  timer -= 1;   
   TextView timer1 = (TextView)findViewById(R.id.textView1);        
   timer1.setText(""+timer);
}

public void onFinish()
{
    //PERFORM END ACTION UPON FINISHING THE GAME
    endtime = 120 - timer;
    TextView endtime1 = (TextView)findViewById(R.id.textView2);
    1.setText(""+endtime);
}
}

解决方案

First *: this is your class that extended the* CountDownTimer :

public class GameTimer extends CountDownTimer{

private YourActivity context;
private int timer = 120;
private int endtime;
private TextView timer1,endTime1;

public gameTimer(YourActivity context, long startTime, long interval)
{
    // passing the context of your activity
    this.context = context;

    //get the textView to display results
    timer1 = (TextView)this.context.findViewById(R.id.textView1); 
    endTime1 = (TextView)this.context.findViewById(R.id.textView2);

    super(startTime, interval);
}

public void onTick(long millisUntilFinished)
{
   //on tick deduct timer by 1
  timer -= 1;  

  //override the method runOnUIThread 
  context.runOnUiThread(new Runnable(){

      @override
      public void run(){
         timer1.setText(""+timer);
      }
  });        

}

public void onFinish()
{
    //PERFORM END ACTION UPON FINISHING THE GAME
    endtime = 120 - timer;

     //override the method runOnUIThread 
  context.runOnUiThread(new Runnable(){

      @override
      public void run(){
         endTime1.setText(""+endtime);
      }
  });

}
}

Second : in your activity ( on the method onCreate() , instanciate your GameTimer and start it :

//instanciate the GameTimer and pass the context to it
GameTimer gameTimer = new GameTimer(this, 120000, 1000);
    gameTimer.start();

这篇关于安卓:定时器onTick问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆