3

I have 3 buttons on the UI. On, Off and SOS(flash at repeated intervals). If I press On and then the Off button the flash turns off as expected.But if I press SOS(it flashes as expected at regular intervals) and then Off it refuses to stop flashing. The code is as follows:

SOSbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    try {
                SOSon = true;
                startSOS();
                onSOSPress();
            } catch (Exception ex) {
                throw ex;
            }
            }
        });
void onSOSPress() {
            try {
              Flashlight flashlight=new Flashlight();
                SOSon = true;
                   flashlight.Flashthread = new Thread(new Runnable() {
                            @Override
                            public void run() {
                                for (int i = 0; i < System.currentTimeMillis(); i++) {
                                    while (!FlashThreadStop) {
                                        if (FlashOn) {
                                           myParameters.setFlashMode(Parameters.FLASH_MODE_OFF);
                                            try {
                                                myCamera.setParameters(myParameters);
                                            }
                                            catch (Exception ex)
                                            {
                                                //logger.log(Level.SEVERE, "an exception was thrown", ex);
                                            }
                                           myCamera.stopPreview();
                                           FlashOn = false;
                                        } else {
                                            TurnOnFlash();
                                        }
                                        try {
                                            Thread.sleep(100);
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                    }

                                }
                            }
                        });
                       flashlight.Flashthread.start();
            } catch (Exception ex) {
                throw ex;
            }
        }

and the TurnOff mechanism is as follows:

off.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (FlashOn) {

                    // turn off flash
                    TurnOffFlash();

                } 
            }
        });

private void TurnOffFlash() {

        if (FlashOn) {

            if (myCamera == null || myParameters == null) {
                return;

            }

            myParameters = myCamera.getParameters();
            myParameters.setFlashMode(Parameters.FLASH_MODE_OFF);
            myCamera.setParameters(myParameters);
            myCamera.stopPreview();
            FlashOn = false;

        }
    }
void endSOS(){
        FlashThreadStop=true;
    }
    void startSOS(){
        FlashThreadStop=false;
    }

EDIT: The method onSOSPress() has been updated to reflect the working SOS mode(OFF button works now)

Anurag Joshi
  • 300
  • 1
  • 12
  • Try this: http://stackoverflow.com/questions/6068803/how-to-turn-on-camera-flash-light-programmatically-in-android – Hadži Lazar Pešić Aug 20 '16 at 11:24
  • Thank you for your reply! I had earlier tried that link which helped me to turn on and off the flash in the first place. My problem persists with the flash not being turned off when I am in SOS mode but turns off when I am in normal "ON" mode. Maybe its a problem with the threading or so in my onSOSPress method but I am not quite sure about solving this as I am new to Android! – Anurag Joshi Aug 20 '16 at 11:29

1 Answers1

0

Your TurnOffFlash method does not cover flash blinking very well. You are not stopping the thread so it continues flashing back and forth. You need to add one line to cover that part as well:

Flashthread.interrupt();

This way you'll keep your functionality turning off the flash, and if the thread is running the SOS mode, you'll stop it as well.

Side note: You're failing to follow the common Java nomenclature where methods and variables start with a lowercase letter and use camelCase.

Vucko
  • 7,371
  • 2
  • 27
  • 45
  • Thank you for your reply! I added these 2 lines to the TurnOffFlash method but it did not seem to solve the issue as it still does not stop the LED flashing when on SOS mode. if(SOSon) Flashthread.interrupt(); – Anurag Joshi Aug 20 '16 at 12:52
  • 1
    Run in debug mode and see whether it enters the functions as you expect it to. This might not be the solution at the moment, but it is the general idea to move you in the right direction. See how to stop a thread differently. – Vucko Aug 20 '16 at 12:54
  • Thank you. I was able to solve this issue with some Flags – Anurag Joshi Aug 22 '16 at 13:32