How to stop a timer after certain number of times

后端 未结 3 828
醉话见心
醉话见心 2021-01-12 04:06

Trying to use a Timer to do run this 4 times with intervals of 10 seconds each.

I have tried stopping it with a loop, but it keeps crashing. Have tried

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-12 04:41

    for(int i = 0 ;i<4 ; i++){
        Runnable  runnableforadd ;
        Handler handlerforadd ;
        handlerforadd = new Handler();
        runnableforadd  = new Runnable() {
            @Override
            public void run() {
              //Your Code Here
                handlerforadd.postDelayed(runnableforadd, 10000);                         } 
        };
        handlerforadd.postDelayed(runnableforadd, i);
    
    }
    

提交回复
热议问题