Search code examples
androidtimerrunnable

Strobe light for android


I've searched on stackoverflow and used the algo that uses Handler and postDelayed messages to turn on and turn off the light.

The algo runs perfectly.... here is what I did:

if(mActive)//control the runnable thread
    {
        if(mSwap)//toggler for strobe
        {
            //Turn Flash On
            cam = Camera.open();
            Parameters p = cam.getParameters();
            p.setFlashMode(Parameters.FLASH_MODE_TORCH);
            cam.setParameters(p);
            cam.startPreview();
            mSwap = false;
            mHandler.postDelayed(mRunnable, 1);
        }
        else
        {
            //Turn Flash Off

            cam.stopPreview();
            cam.release();
            cam = null;
            mSwap = true;
            mHandler.postDelayed(mRunnable, 1);


        }
    }

but I cannot increase the switching to match the strobe frequency of those apps that are on the market...

Is there any other logic that I can use(instead of queuing the messages )?? Will using a timer give better result?

Thanks


Solution

  • In your code you are allocating and deallocating the Camera object each time you switch on/off your torch. This takes quite some time every time.

    To fix, move the allocation / deallocation to the onStart and onStop methods and only do the bare minimum to switch the light on and off.