Reputation: 15
cameraOn();
while (counter == 1){
if(counter == 0){
cameraOn();
counter += 1;
}else{
cameraOff();
counter -= 1;
}
}
The methods are:
private void cameraOff() {
// TODO Auto-generated method stub
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.release();
camera = null;
}
private void cameraOn() {
// TODO Auto-generated method stub
camera = Camera.open();
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
}
and I have a button who have to break the loop and finish the activity:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (camera == null){
finish();
}else{
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.release();
camera = null;
finish();
}
I'm a beginner and I don't understand how does the loop work, I tought with the counter I could do it, but it didn't work. What I'm trying is to make the camera flash blink every second in a loop until I will press the button. please help me. Thanks
Upvotes: 0
Views: 117
Reputation: 6510
As others have said, entering a loop only when counter == 0 means it will never enter the inner loop requiring counter == 1, and never enter cameraOn().
Try this:
Clear out your onClick method and replace it with:
@Override public void onClick(View v) {
cameraOff();
/* or 'toggle();' if you prefer, see below */
}
(Since it appears to do exactly the same things.)
Comment out / delete that whole nested loop, and be sure to call cameraOn() or toggle() somewhere to get things started.
For setting up the toggle, you can add a static Boolean isFlashActive;
(or otherwise detect the flash state, I haven't used that api yet)
..and add a function:
private toggle(){
if ( isFlashActive ) {
cameraOff();
} else {
cameraOn();
}
/* delay? */
}
For the toggle delay, you have a couple options:
First, you can call toggle() from another thread via a Runnable
or one of the android options like AsyncTask
while adding a wait() into the toggle function to provide the delay;
Second is my personal favorite, which is to setup an intent receiver then use setRepeating()
with a PendingIntent
.
Upvotes: 1
Reputation: 5158
The following part of your code will never be executed because you only enter the while block if counter == 1.
if(counter == 0){
cameraOn();
counter += 1;
So if you ever entered your while block while (counter == 1)
, you will always end up calling cameraOff() method. In other words your if statement will never be true and the else statement will be the one to be always executed.
Upvotes: 1
Reputation: 1533
if (counter == 0) will never be true as your loop will exit in that case.
Upvotes: 2