Search code examples
javaandroidcamera-flash

Option like SOSModule didn't work


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??


Solution

  • Its because your thread run is not get called

    Try this

     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

    Thread: Not calling run method