Android - run a function every X milliseconds while condition true?
<p>I just want to run a function myFunction() every X milliseconds while an external flag is true. However I've had no luck with Threads (app crashes), Timers (app crashes). Handlers/runnables are a bit better but typically I'll have something like: </p>

<pre><code>Runnable rr = new Runnable() {
                public void run() {
                    if (flag1 == true) {
                        myFunction();
                    } else {
                        return; 
                    }
                }
            }; 
            handler.postDelayed(rr, 1000);
</code></pre>

<p>But then the problem is execution will come one after another after 1000 milliseconds. I want one execution of myFunction to happen, wait 1000ms, call myFunction, wait 1000ms, call myFunction, etc, until flag1 becomes false. </p>

<p>I've been stuck on this for a while, so any help is much appreciated.</p>

<p>EDIT - more question info </p>

<p>The handler is defined as follows: </p>

<p>private Handler handler = new Handler(); </p>

<p>And its class is a BroadcastReceiver where I'm trying to listen for flag changes based on asynchronous events from external hardware.</p>
