0

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;
            }
        }
    });
}
Jan
  • 2,060
  • 2
  • 29
  • 34
Muhammad
  • 13
  • 4

3 Answers3

5

What you are doing is running the process (the loop) in the main thread which causes the UI to be blocked. You have to remember that you must not block the UI in android.

So what you need to do is create another thread for your operations. You can do it using asyncTask or whatever method you know for multi-threading.

What will happen is, in your onClickListener, you will only set the value of isOn. The process (other things inside the if-else statement) must be implemented on another thread.

hehe
  • 1,294
  • 13
  • 26
0

Since camera light on and off logic is too long to fill in the event handler,you should put them outside of it to keep it clean.

 btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(isOn){
               turnOff();
            } else {
               turnOn();
            }
    }  

      private void turnOn() {
        // ... logic on turn on
        isOn = true;
      }

      private void turnOff() {
        // ...logic on turn off
        isOn = false;
      }

Refer to this post on how to turn camera light on and off programmatically.

Community
  • 1
  • 1
zionpi
  • 2,593
  • 27
  • 38
0

Shouldn't your code read:

isOn = true;
while(isOn) {
    // some code
}

instead of :

while(!isOn) {
    // some code
}
isOn = true;
Neuron
  • 5,141
  • 5
  • 38
  • 59