Android flashlight app using camera api 2 [closed]

Follow this link to learn about making flashlight android app.

To make the flashlight blink you can set up your own logic to turn it on and off programmatically.

Edit posting code as asked:

For this you should do like :

  1. Check whether flash light is available or not ?

  2. If yes then Turn Off/On

  3. If no then you can do whatever according to your app. needs

For Checking availability of flash in device:

You can use the following

context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

which will return true if a flash is available, false if not.

Taking code from here:

private void BlinkFlash(){
    String myString = "010101010101";
    long blinkDelay =50; //Delay in ms
    for (int i = 0; i < myString.length(); i++) {
        if (myString.charAt(i) == '0') {
            params = camera.getParameters();
            params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            camera.setParameters(params);
            camera.startPreview();
            isFlashOn = true;



        } else {
            params = camera.getParameters();
            params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
            camera.setParameters(params);
            camera.stopPreview();
            isFlashOn = false;

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

To call this method:

yourbuttonname.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        BlinkFlash();
    }
});

Hope this helps.

Leave a Comment