0

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){}
Pythagoras
  • 25
  • 3

1 Answers1

1

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

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • I implemented this into my code, however the try catch block is now throwing an error. I added the code above and the error is "Unreachable catch block for interrupted exception" Again, thank you for this trouble – Pythagoras Jan 27 '14 at 03:58
  • That's terrible but how can I possibly help if I don't know what the error is? Also, could you please edit your post and add the code? It makes it much easier to read. To be honest, I typically won't even look at code in a comment – codeMagic Jan 27 '14 at 03:59
  • on is an int representing the milliseconds for which the light is on for – Pythagoras Jan 27 '14 at 04:01
  • `sleep()` takes a `long` not an `int`. [See the docs](http://developer.android.com/reference/java/lang/Thread.html#sleep(long)). And you still haven't said what the error is... – codeMagic Jan 27 '14 at 04:03
  • I got it solved, I added the try catch within the Runnable, thank you – Pythagoras Jan 27 '14 at 04:06