I am trying create option like SOS Module in my app, I create code to handle 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 required permissions in manifest and of course I check uses permission on time.
But this no work. I just create other code but working like "one flash" without any cycle.
Can you guys help me?
Guys this is important for me, i cant do this becouse my Huawei p8 Lite and p9 Lite dont give any errors when this happend, its a Huawei software problem, with camera i need test it on psychical device, and its a big problem that i no have any 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 flash is open for infinity without any blink effect Any idea??
You need a flag that tells your thread when to stop.
boolean shouldStop = false;
while (!shouldStop){
if(FlashOn){
...//do SOS stuff
}
}
public void endSOS(){
shouldStop = true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With