An option like SOSModule did not work

I am trying to create a parameter such as the SOS module in my application, I am creating code for this:

class SOSModule { private Camera camera; private Camera.Parameters params; private boolean isFlashOn; void blink(final int delay, final int times) { Thread t = new Thread() { public void run() { try { for (int i=0; i < times*2; i++) { if (isFlashOn) { turnOffFlash(); } else { Camera.open(); turnOnFlash(); } sleep(delay); } } catch (Exception e){ e.printStackTrace(); } } }; t.start(); } void turnOnFlash() { if (!isFlashOn) { if (camera == null || params == null) { return; } params = camera.getParameters(); params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); camera.setParameters(params); camera.startPreview(); isFlashOn = true; } } void turnOffFlash() { if (isFlashOn) { if (camera == null || params == null) { return; } params = camera.getParameters(); params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); camera.setParameters(params); camera.stopPreview(); isFlashOn = false; } } 

}

I also add all the necessary permissions to the manifest, and of course I check the usage on time.

But this is not work. I am just creating another code, but working as a "one flash" without any loop.

Can you guys help me?

Guys, this is important for me, I can’t do it because my Huawei p8 Lite and p9 Lite do not give any errors when this happens, there is a problem with the Huawei software, with the camera that I have to check on the psychic device, and it The big problem is that I don't have logs from devices.

  public void flash_effect() throws InterruptedException { cam = Camera.open(); final Camera.Parameters p = cam.getParameters(); p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); Thread a = new Thread() { public void run() { for(int i =0; i < 10; i++) { cam.setParameters(p); cam.startPreview(); try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } cam.stopPreview(); try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }; a.start(); }} 

This code worked, but the flash is open for infinity without any blink effect Any ideas ??

+6
source share
2 answers

This is because your thread is not called

try it

  void blink(final int delay, final int times) { Thread t = new Thread(new Runnable() { @Override public void run() { try { for (int i=0; i < times*2; i++) { if (isFlashOn) { turnOffFlash(); } else { turnOnFlash(); } Thread.sleep(delay); } } catch (Exception e){ e.printStackTrace(); } } }); t.start(); } 

you can read more here

Subject: Do not call the start method

0
source

You need a flag that tells you about your thread when to stop.

 boolean shouldStop = false; while (!shouldStop){ if(FlashOn){ ...//do SOS stuff } } public void endSOS(){ shouldStop = true; } 
+1
source

Source: https://habr.com/ru/post/1013225/


All Articles