0

Set timer for each loop means if there are 3 items in an array and to access those we need call forloop. What I want there is when the 1st loop is running it should run up to a specific time. Then the second loop run with the given specific time to it. I already tried with the time delay with Handler but it is not working. In Handler, all loop run at once but execute after the specific time. I don't know how to set the timer for each loop.

 {
    "tp_id": 85,
    "id": 15,
    "therapy_type": 1,
    "mode": 0,
    "level": 5,
    "duration": 7
  },
  {
    "tp_id": 85,
    "id": 16,
    "therapy_type": 2,
    "mode": 3,
    "level": 4,
    "duration": 1
  },
  {
    "tp_id": 85,
    "id": 17,
    "therapy_type": 1,
    "mode": 3,
    "level": 4,
    "duration": 1
  }

I am accessing the duration value from these above arrays and want to run each loop with their specific duration. I tried with time delay but not working as I want.

   for(int i=0;i<type.size();i++){

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Log.e("chekkk",""+i);
                    }
                }, duration.get(i) * 1000);
            }
  • 1
    Possible duplicate of [How to delay a loop in android without using thread.sleep?](https://stackoverflow.com/questions/20896245/how-to-delay-a-loop-in-android-without-using-thread-sleep) – AskNilesh Apr 16 '18 at 11:07
  • try to use `ExecutorService` from [concurrency framework](http://www.vogella.com/tutorials/JavaConcurrency/article.html) to run one after other – anatoli Apr 16 '18 at 11:40
  • @NileshRathod, No it is not duplicate of that one because I have already go through that question. – Sudhansu Sekhar Apr 16 '18 at 11:47

2 Answers2

0

Try post the Runable itself.

//Handler should be saved in object or other way. 
//You must remove all timers and destory it exciplit. 
//Otherwise the Handler and Timers will be leak.
Handler mTimerHandle = new Handler(); 
Long startTime = System.currentTimeMillis();
//...
for(int i=0;i<type.size();i++){
    Runable timer; //You can save it if you want to cancel it.
    long timerDuration = long(duration.get(i) * 1000); //make a variable in for-loop's scope instead i;
    timer = new Runnable() {
        @Override
        public void run() {
            Log.e("chekkk",""+i);
            long nextEmit = (System.currentTimeMillis() - startTime) % timerDuration;
            long passedTime = ((System.currentTimeMillis() - startTime) / timerDuration ) * timerDuration;
            mTimerHandle.postAtTime(timer, passedTime  + nextEmit);
        }
    };

    mTimerHandle.postAtTime(timer , startTime + timerDuration);
}
WinCloud
  • 183
  • 7
0

in your code

a : if the first duration be a 7 and multiply to 1000 = 7000 = 7s

b : if second one be a 2 and multiply to 1000 = 2000 = 2s

after 2 second (b) gonna run and after 7 sec or 5 sec after b (a) gonna run

if you want to (b) fired after a you should do the math for duration of the (b) !

for example if post delayed of the a equals to 7s than post delayed of the b should equal to ( 7 + 2 )

you can edit your code like this :

 int totalDuration=0;

 for(int i=0;i<type.size();i++){

                totalDuration+=duration.get(i);

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Log.e("chekkk",""+i);
                    }
                }, totalDuration * 1000);
            }
Amir Hossein Mirzaei
  • 2,325
  • 1
  • 9
  • 17