Create a Flashlight application with SOS mode. It has 3 buttons (On, Off and SOS). The application works in the usual On and Off mode, but not in SOS mode (SOS mode does not turn off)
//this method gets called when Off button is pressed 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(); try { if (SOSon) Flashthread.interrupt(); SOSon = false; } catch (Exception ex) { throw ex; } FlashOn = false; number_of_press=0; } }
and Flashthread
used here
void onSOSPress() { if (number_of_press == 1) { try { SOSon = true; if (!Flashthread.isInterrupted()) { if (SOSon) { Flashthread = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < System.currentTimeMillis(); i++) { if (FlashOn) { myParameters.setFlashMode(Parameters.FLASH_MODE_OFF); myCamera.setParameters(myParameters); FlashOn = false; } else { TurnOnFlash(); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }); Flashthread.start(); } } else Flashthread.resume(); } catch (Exception ex) { throw ex; } } }
In the turnOffFlash
method, since I read that the interrupt method does not actually βinterruptβ / kill the thread, which I can use instead of Thread.Interrupt();
so that pressing the Off
button stops SOS
> mode? I tried stop()
and destroy()
, but both of them crashed the application.
source share