Repeat code in android
<p>I want an app which changes background color when pressing a button. After 500 ms, I want to change background color to black for 2000ms. And then repeat whole process again, until user terminates that.</p>

<p>I have following code but its not working as I think it should.</p>

<pre><code>private void set() {
    rl.setBackgroundColor(Color.WHITE);
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    rl.setBackgroundColor(Color.BLACK);
                    set(); // can I do that?
                }
            });             
        }
    }, 500);    
}
</code></pre>

<p>Can someone point me to right direction how can I do that? So I want:</p>

<ol>
<li>Execute some code</li>
<li>After X time passed by , I want to execute another code and it should stay that way for X amount of time</li>
<li>Repeat process until user cancels that.</li>
</ol>
