Stop thread on Android
<p>I have this code for changing the screen color.</p>

<p>But I don't know how to stop this thread:</p>

<pre><code>lin = (LinearLayout)findViewById(R.id.mainlayout);
        new Thread(new Runnable() {
            public void run() {
                while (finalStatus &lt; 1) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    colorHandler.post(new Runnable() {
                        public void run() {
                            if(flag)
                            {
                             lin.setBackgroundColor(Color.BLUE);
                             flag = false;
                            }
                            else
                            {
                                lin.setBackgroundColor(Color.RED);
                                 flag = true;
                            }
                        }
                    });
                }
            }
        }).start();
</code></pre>

<p>I tried to put:</p>

<pre><code>Button btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                Toast.makeText(getBaseContext(),"STOP",Toast.LENGTH_SHORT).show();
                finalStatus=1;
                lin.setBackgroundColor(Color.BLACK);
            }
        });
</code></pre>

<p>and the thread stopped, but the screen is red, or blue, I need black.</p>

<p>Moreover, how to start this thread after I have stopped it?</p>
