Search code examples
androidtimercountdown

Running codes in Countdown Timer


I'm creating an app that connect with server(localhost).

In my Activity, I have a countdown timer which will tick for 30 seconds and at the finish() I have some code to check if there's any changes in the database on server, and then start the timer again so it'll be a loop in the activity. But the timer seem like running just once. When I'm running in debug mode, the timer get some error at the finish(), it said that I'm missing some folder or file that related to the API or the SDK version which is being run in android:targetSdkVersion="9"

Please help me with this issue. Here's the activity:

public class Account_Activity extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
   //Creating the UI and some methods
   counter = new CountDownTimer(30000, 1000) {

       @Override
       public void onTick(long millisUntilFinished) {

       }       

       @Override
       public void onFinish() {
           //Codes for checking the changes in the database on the server
           start(); 
       }
   };
   counter.start();
}
}

Do I have to make a thread or not?


Solution

  • public class Account_Activity extends Activity {
       @Override
       protected void onCreate(Bundle savedInstanceState) {
       //Creating the UI and some methods
       counter = new CountDownTimer(30000, 1000) {
    
           @Override
           public void onTick(long millisUntilFinished) {
    
           }       
    
           @Override
           public void onFinish() {
               //Codes for checking the changes in the database on the server
               new CountDownTimer(30000, 1000).start(); <------Start Again.
           }
       };
       counter.start();
    }
    }