0

I am trying to make an Android app that blinks the camera LED when the user presses the blink button. I'm able to blink the LED. I have added another button to stop the blinking of the LED when the user wants, but I am not able to stop the blinking.

What should I add to stop the blinking of the LED?

Here's my code:

import android.app.Activity;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {


    Button blinkled,stop;
    private Camera camera;
    private boolean isFlashOn;
    private boolean hasFlash;
    Parameters params;

    String myString;
    long blinkDelay =200;
    int i;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getActionBar().hide();


        blinkled= (Button) findViewById(R.id.blink);
        stop= (Button) findViewById(R.id.stop);

        // get the camera
        getCamera();

        blinkled.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Runnable r = new Runnable() {
                    @Override
                    public void run() {

                        long start = System.currentTimeMillis();
                        long end = start + 5 * 1000;
                        while (System.currentTimeMillis() < end) {


                            for (i = 0; i < 2; i++) {
                                if (i % 2 == 0) {
                                    turnOnFlash1();
                                } else if (i % 2 == 1) {
                                    turnOffFlash1();
                                }


                                try {
                                    Thread.sleep(blinkDelay);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                };

                Thread th = new Thread(r);
                th.start();
            }
        });


    }

    /*
     * Get the camera
     */
    private void getCamera() {
        if (camera == null) {
            try {
                camera = Camera.open();
                params = camera.getParameters();
            } catch (RuntimeException e) {
                Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
            }
        }
    }

    private void turnOnFlash1() {
        if (!isFlashOn) {
            if (camera == null || params == null) {
                return;
            }


            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_TORCH);
            camera.setParameters(params);
            camera.startPreview();
            isFlashOn = true;


        }

    }

    private void turnOffFlash1() {
        if (isFlashOn) {
            if (camera == null || params == null) {
                return;
            }


            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_OFF);
            camera.setParameters(params);
            camera.stopPreview();
            isFlashOn = false;


        }
    }

    if (camera != null) {
        camera.release();
        camera = null;
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
thisisbhavin
  • 344
  • 3
  • 15

2 Answers2

0

To stop the blinking, you need to stop the runnable on which you started the blinking. This is how you can stop the runnable : https://stackoverflow.com/a/19894653/1239966

Community
  • 1
  • 1
Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
  • I'm not sure that is his issue because the last condition that will be met is `1%2==1` which will call the turn off function so even if the runnable keeps going after the second iteration the torch should already be turned off. – Max Worg Jan 25 '15 at 13:34
  • But then it'll go back to the while loop and execute again ? – Shivam Verma Jan 25 '15 at 13:36
  • I don't think so because each runnable has 2 conditions in which it will call the on/off methods the first is a time constraint and the second is the condition `for (i = 0; i < 2; i++)` so each runnable will only call the on/off functions `i<2` times. Even if the runnable still runs the condition i<2 will still be honored because `i` will continue to increase and will no longer be <2. – Max Worg Jan 25 '15 at 13:42
  • What about this loop : `while (System.currentTimeMillis() < end) {..}` This'll run again and i goes back to 0. – Shivam Verma Jan 25 '15 at 13:44
  • Ah. I see. Yes, you are correct it will continue execution and `i` is reset to `0`. But I don't think that the runnable is the issue because inside the runnable the code to turn off the torch is called but the torch never turns off if I understand correctly. – Max Worg Jan 25 '15 at 13:49
  • He still needs to cancel the runnable otherwise the led will be turned on again by the runnable. – Shivam Verma Jan 25 '15 at 13:53
  • The runnable will stop itself when `while (System.currentTimeMillis() < end)` evaluates to `false`. There is no need to terminate it before that condition is met because there is a reason to let the runnable run for 5 seconds. But the real issue is why the torch is not blinking/turning off during those 5 seconds. Forcing the thread to close before the 5 seconds is up doesn't address the issue of why the turnOff function is not executing as expected. – Max Worg Jan 25 '15 at 14:03
  • Ah! Alright :) I am not even sure if OP still needs the answer xD – Shivam Verma Jan 25 '15 at 14:05
0

This might not be what you are referring to but your stop button stop= (Button) findViewById(R.id.stop); is not hooked up to any OnClickListener so if you are trying to stop the blinking by hitting the stop button your code doesn't know how to react yet until you assign a listener (just like you did with the blinkled button.

On another note the following code seems to be misplaced:

if (camera != null) {
        camera.release();
        camera = null;
    }

It's not enclosed in any of the functions so most likely it's not being run when you expect it to.

Max Worg
  • 2,932
  • 2
  • 20
  • 35