I am new to android programming. I am working on an app which will make the flashlight blink when the button is pressed and turn it off when the button is pressed again.

Currently I am stuck in an infinite loop where I can't understand how to give it a command to end the loop when the button is pressed again.

```
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    camera = Camera.open(); //opening camera hardware
    params = camera.getParameters(); //getting parameters

    //finding elements
    final Button btn = (Button) findViewById(R.id.btn);
    //accessed within inner class at onClick , so it must be declared final

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(isOn){
                params.setFlashMode(Parameters.FLASH_MODE_OFF);
                camera.setParameters(params);
                isOn=false;
                btn.setBackgroundResource(R.drawable.btnoff);
            } else {
                while(!isOn) {
                    params.setFlashMode(Parameters.FLASH_MODE_TORCH);
                    camera.setParameters(params);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    params.setFlashMode(Parameters.FLASH_MODE_OFF);
                    camera.setParameters(params);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                btn.setBackgroundResource(R.drawable.btnon);
                isOn=true;
            }
        }
    });
}

```