0

I have tried multiple ways to have a single persistent timer update the ui in multiple activities, and nothing seems to work. I have tried an AsyncTask, a Handler, and a CountDownTimer. The code below does not execute the first Log.i statement.... Is there a better way to start the timer (which must be called from another class) in Main (which is the only persistent class)?

 public static void MainLawTimer()
{
    MainActivity.lawTimer = new CountDownTimer(MainActivity.timeLeft, 1000) 
    {
           public void onTick(long millisUntilFinished) 
           {
               Log.i("aaa","Timer running. Time left: "+MainActivity.timeLeft);
              MainActivity.timeLeft--; 

              if(MainActivity.timeLeft<=0)
              {
                //do stuff
              }
              else
              {
                  //call method in another class                          
              }  
           }
public void onFinish() 
           {  }
    }.start();
}

To clarify my problem:

When I run the code the Log.i("aaa","Timer running") statement is never shown in the log, and the CountDownTimer never seems to start. MainLawTimer is called from another class only (not within the same class.

Rilcon42
  • 9,584
  • 18
  • 83
  • 167
  • "is there a better way to start the timer (which must be called from another class) in Main (which is the only persistent class)?". can you elaborate a bit more? – Raghunandan Jun 30 '13 at 04:36
  • @Rilcon42 you cannot update the UI from the timer Thread, you need UI thread to update the UI, and you need to start the timer in your code. – PiyushMishra Jun 30 '13 at 05:47
  • @PiyushMishra, I started the timer in my code, I just omitted the unused OnFinish method and the .start because I was trying to provide only the essentials.... I have added it back into the code above. – Rilcon42 Jul 01 '13 at 00:47
  • @Rilcon42 timer is not meant to update the UI, you have to use runOnUiThread(action); can see this for more information http://stackoverflow.com/questions/4921286/simple-timer-example-but-it-wont-work-fine – PiyushMishra Jul 01 '13 at 05:01
  • @PiyushMishra: of course you can use CountDownTimer. in the UI thread since it does not create any orher thread (it uses a Handler to make time ticks) – pskink Jul 01 '13 at 13:17

2 Answers2

1

For CountDownTimer

http://developer.android.com/reference/android/os/CountDownTimer.html

You can use a Handler

Handler m_handler;
Runnable m_handlerTask ; 
int timeleft=100;
m_handler = new Handler(); 
@Override
public void run() {
if(timeleft>=0)
{  
     // do stuff
     Log.i("timeleft",""+timeleft);
     timeleft--; 
}      
else
{
  m_handler.removeCallbacks(m_handlerTask); // cancel run
} 
  m_handler.postDelayed(m_handlerTask, 1000); 
 }
 };
 m_handlerTask.run();     

Timer

  int timeleft=100;
  Timer _t = new Timer();  
  _t.scheduleAtFixedRate( new TimerTask() {
            @Override
            public void run() {

               runOnUiThread(new Runnable() //run on ui thread
                 {
                  public void run() 
                  { 
                    Log.i("timeleft",""+timeleft);  
                    //update ui

                  }
                 });
                 if(timeleft>==0)
                 { 
                 timeleft--; 
                 } 
                 else
                 {
                 _t.cancel();
                 }
            }
        }, 1000, 1000 ); 

You can use a AsyncTask or a Timer or a CountDownTimer.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Thank you for the examples, I will try them. Did you see an error in my code above that would have prevented the way I tried to do it from working? – Rilcon42 Jul 01 '13 at 00:51
0

Thank you all for your help, I discovered the error in my code... timeLeft was in seconds rather then milliseconds. Since timeLeft was under 1000 (the wait period) the timer never started.

Rilcon42
  • 9,584
  • 18
  • 83
  • 167