Pregunta

So I have this really weird problem that I simply cannot understand why it doesn't work. I am building a strobe light as part of my app and have created a separate Strobelight class. When I call the turnOn method or update method, the intervals never are changed. I guess it would make it easier to explain using some code:

public class Strobelight{

private int delayOn, delayOff;

public void turnStrobeOn(){...}

public void update(int a_delayOn, int a_delayOff){
    delayOn = a_delayOn;
    delayOff = a_delayOff;
}

public void turnOn(int a_delayOn, int a_delayOff){
    delayOn = a_delayOn;
    delayOff = a_delayOff;
    this.turnStrobeOn();
}

Depending on whether the strobe light is already on or not, one of these methods are called to change turn on the strobe light with the specified intervals or simply to change the intervals.

Instead of changing the intervals to something custom, the application just uses the smallest possible intervals when calling Thread.sleep() for the flashlight being on or off

EDIT: this is the thread code, and code that turns the flashlight on

public void turnStrobeOn(){

    for ( int i = 0; i < 3; i++){

        isInCycle = true;

        cam = Camera.open();
        Parameters p = cam.getParameters();
        p.setFlashMode(Parameters.FLASH_MODE_TORCH);
        cam.setParameters(p);
        cam.startPreview(); // the flashlight is now on
        lightIsOn = true;
        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(delayOn);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        cam.stopPreview();
        cam.release();
        lightIsOn = false;
        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(delayOff);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    } //end of for loop

        isInCycle = false;  

} // end of turnStrobeOn
¿Fue útil?

Solución

You might want to get someone else's advice on this buy my best guess is that you can't be so specific on sleep(). Also don't forget to use miliseconds for that. I'd suggest using the android handler and doing postDelayed for this instead of sleeping.

How to pause / sleep thread or process in Android?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top