Search code examples
androidandroid-handler

how can I call function every 10 sec?


I want make an app that call a function for example every 10 sec.

I wrote my code like this:

Handler ha=new Handler();
ha.postDelayed(new Runnable() {
    @Override
    public void run() {
        //call function

    }
}, 10000); 

But my function call just one time in 10 sec after compile this code.

How can I fix it?


Solution

  • Do it like this:

    final Handler ha=new Handler();
    ha.postDelayed(new Runnable() {
    
        @Override
        public void run() {
            //call function
    
            ha.postDelayed(this, 10000);
        }
    }, 10000);