Question

The problem I am having right now is that when I click a button in my app, that app stays pressed down and I cannot click any other buttons, such as one to stop the thread.

Right now the app is just flashing the camera light (strobe light) and it utilizes Thread.sleep(int) when waiting for the next flash.

Is there anyway I can run this simple operation in another thread or enable the other buttons?

Thanks!

EDIT: (This is how it looks in the try catch with a new runnable thread, the try catch is throwing an error)

        try{
            cam = Camera.open();
            Parameters p = cam.getParameters();
            p.setFlashMode(Parameters.FLASH_MODE_TORCH);
            cam.setParameters(p);
            cam.startPreview();

            new Thread(new Runnable() {
                public void run() {
                        Thread.sleep(on);
                }
            }).start();

            camIsOn = true;
        } catch(InterruptedException ie){}
Was it helpful?

Solution

Is there anyway I can run this simple operation in another thread or enable the other buttons?

Yes, don't call Thread.sleep() on the UI Thread or the UI Thread will sleep and you won't be able to do any UI stuff the entire time. Something like

new Thread(new Runnable() {
    public void run() {
            // do stuff that doesn't touch the UI here
    }
}).start();

code borrowed from Mr. Murphy here

You also might want to read the docs here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top