2

I'm trying to take a photo with the Android camera. I need to wait 1 second or so when the program first starts so that Preview can be initiated and the photo can be taken. I did that with Handler.postDelayed and it works fine.

Now, my issue is I'd like to PAUSE the flow of the program until the run() gets executed, in which I used a while(true) loop with a flag to signal that the method has finished. However, the program freezes. There's no error returned. Can anyone shed some lights ? Below is my code

 flag = false;
 handler.postDelayed(new Runnable() { 
             public void run() 
             { 
                 preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
                 preview.camera.startPreview();
                 flag = true;
             } 
        }, 1000);

        while (true)
        {
            if (flag) break;
        }
akka243
  • 25
  • 1
  • 1
  • 4
  • 1
    Save your Runnable as variable and then call handler.removeCallbacks(your runnable variable) when need to cancel the launch of it – Volodymyr Lykhonis Nov 20 '12 at 22:40
  • thanks for the response, but you seem to misunderstood my question. I would like the handler to terminate itself after 1 second, and I want to hold the flow of the code until run() has finished its job. – akka243 Nov 20 '12 at 22:43
  • Well, then you do not need handler at all, just call Thread.sleep or use mutex (Object notify/wait) – Volodymyr Lykhonis Nov 20 '12 at 22:45
  • I've tried Thread.sleep(x miliseconds); and it does not work. I've searched through other posts and people suggest to use either a flag to signal, or the Handler – akka243 Nov 20 '12 at 22:48
  • It works. If not, then it is impossible to suggest something else, because no code and/or exceptions you get. – Volodymyr Lykhonis Nov 20 '12 at 22:49
  • 1
    Do **NOT** pause on the main UI thread, with sleep or while or whatever. If you think you need to do that, you're wrong. What you need to do is restructure your code so that things don't happen until your handler has executed. – Joseph Earl Nov 20 '12 at 22:50

1 Answers1

1

Remove the while(true) and it should continue, let me explain:

Android has a queue of tasks, so when finishs the current task it will go to the next task, so in your case the PostDelayed will be executed after it finishs the current Infinity loop, which will never be done, because the loop is blocking your Runnable in the queue.

so the best way is remove the Infinity loop, and in the end of your runnable call a method that continues your flow or a listener.

Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77
  • Are you sure? I think he wants to keep looping until the flag is true. The problem is probably that the value of flag is cached as false, and that it needs to be volatile. – corsiKa Nov 20 '12 at 23:10
  • yes, I tried to Log the value of flag and it's always false, which means run() never gets executed. – akka243 Nov 20 '12 at 23:11
  • never use blocking ways (like you did) you must use callbacks using listeners. – Mohammad Ersan Nov 21 '12 at 06:49