2014-03-04 79 views
2

我得到了socket.io的通知,在我的android應用程序中啓動倒數計時器。我使用Handler將數據從socket.io回調發送到UI線程。處理程序的消息完美。但從處理程序啓動倒計時器不起作用(不會調用onTick()函數)。如果我用UI元素啓動它,一切都會好的。當倒數計時器不是從UI線程操作時,最好的方法是什麼?從處理程序開始倒數計時器的問題

mHandler = new Handler(){ 
     public void handleMessage(Message inputMessage) { 

      mBattle = (Battle) inputMessage.obj; 

      switch (inputMessage.what) { 

      case NO_BATTLE: 
       System.out.println("got message NO_BATTLE"); 
       break; 
      //................. 

      case START_BATTLE: 
       startCountdownTimer(mBattle.getCountdown()); 
       System.out.println("got message START_BATTLE"); 
       break; 
     } 
    } 

和倒數計時功能:

private void startProgressBar(final int time){ 
    if (time != 0) { 
     new CountDownTimer(time, 1000) { 

      @Override 
      public void onTick(long millisUntilFinished) { 
       Log.v("Log_tag", "Tick of Progress " + i + " " + 
         + millisUntilFinished); 
       i++; 

      } 
      @Override 
      public void onFinish() { 
       i=0; 
      } 
     }.start(); 
    } 
+0

使用mHandler而不是CountDownTimer來計數diwn – pskink

回答

0

你打印出的time值時,它不工作?如果time < SystemClock.elapsedRealtime() + 1000,那麼將調用onFinish而不調用onTick

+0

thx ..;)我只是愚蠢的,忘記與1000乘以秒。當我試着用按鈕它總是已經成倍 – kokash