0

I have the following code which runs a task every second, but I also want the task to stop after 10 seconds. Can this logic be implemented with a handler in which I'm using? I've tried implementing a counter with a while loop but couldn't get it to work

mHandler = new Handler();
mUpdateUI = new Runnable() {
    public void run() {
        mVistaInspectionDate = HousingFragment.getVistaInspectionDate();
        mVistaInspectionDateTextView.setText(mVistaInspectionDate);     

        if (mVistaInspectionDate != null) {
            mHandler.removeCallbacks(mUpdateUI);
        }
            mHandler.postDelayed(mUpdateUI, 1000); // 1 second
    }
};  

mHandler.post(mUpdateUI); 

4 Answers4

0

Use a While loop with a boolean variable and set this variable to true, then you could count how many times your task has run and stop the task for 1 sec after each run but it could happen that the delay for 1 sec is not one sec because of thread sheduling.

So you could use the time to count and stop the while loop. Save your curennt time and every 1 second will your task executed. After 10 times your Thread terminate, either throu terminating the thread in the while loop or to set the boolean variable to false.

Baalthasarr
  • 377
  • 1
  • 13
0

Try using Timer class, or Quartz scheduler if you can have external dependencies, rather than coding the scheduling and threading logic in your own application.

Amir Moghimi
  • 1,391
  • 1
  • 11
  • 19
0

In order to use a counter, you can wrap it in a function like this:

private void postDelayedWrapped(final int counter, int delay) {
    if (counter <= 0) return;
    mUpdateUI = new Runnable() {
        public void run() {
            mVistaInspectionDate = HousingFragment.getVistaInspectionDate();
            mVistaInspectionDateTextView.setText(mVistaInspectionDate);

            if (mVistaInspectionDate != null) {
                mHandler.removeCallbacks(mUpdateUI); //necessary?
            }
            postDelayedWrapped(counter - 1, 1000);
        }
    };

    mHandler.postDelayed(mUpdateUI, delay);
}

And you start it like this:

mHandler = new Handler();
postDelayedWrapped(10,0);
Chrisport
  • 2,936
  • 3
  • 15
  • 19
0

How about subclassing the Handler class and use an instance of this class keeping track of how many times postDelayed() was called?

   public class MyHandler extends Handler {
        private int maxTimes;
        private int currentTimes; 
        private Runnable runner;
        public Handler(int maxTimes) {
              this.maxTimes = maxTimes;
        }

        @Override
        public void post(Runnable runner) {
              this.runner = runner;
        }

        @Override
        public void postDelayed(Runnable runner,long ms) {
               if (currentTimes == maxTimes) {
                    this.removeCallbacks(runner);
               } else {
                    super.postDelayed(runner,ms);
                    currentTimes++;
               }
        } 


   }

   mHandler = new MyHandler(10);//using subclass instance
   //from here on is the same as the original code.
   mUpdateUI = new Runnable() {
   public void run() {
      mVistaInspectionDate = HousingFragment.getVistaInspectionDate();
      mVistaInspectionDateTextView.setText(mVistaInspectionDate);     

      if (mVistaInspectionDate != null) {
          mHandler.removeCallbacks(mUpdateUI);
      }
         mHandler.postDelayed(mUpdateUI, 1000); // 1 second
    }
  };  

  mHandler.post(mUpdateUI); 
AMC
  • 130
  • 8