how can I call function every 10 sec?

user3257386 picture user3257386 · Feb 4, 2014 · Viewed 29.1k times · Source

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?

Answer

Rudi picture Rudi · Feb 4, 2014

Do it like this:

final Handler ha=new Handler();
ha.postDelayed(new Runnable() {

    @Override
    public void run() {
        //call function

        ha.postDelayed(this, 10000);
    }
}, 10000);