How to use scheduleAtFixedRate for executing in each second

前端 未结 3 857
谎友^
谎友^ 2020-12-22 09:39

in the below code send() function is executing many times in a second,i want to execute send() once in a second,how i change the code

timer.scheduleAtFixedRa         


        
3条回答
  •  猫巷女王i
    2020-12-22 10:15

    I replaced timers with Runnables/Handlers recently, it's much easier

    //declare at top of your activity
    private Handler h = new Handler();
    
    private Runnable myRunnable = new Runnable() {
       public void run() {
        //do stuff  
    
            //run again in one second
        h.postDelayed(myRunnable, 1000);
       }
    };
    
    //trigger the runnable somewhere in your code e.g. onClickHander or onCreate etc
    h.postDelayed(myRunnable, 1000);
    

提交回复
热议问题