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)