-3

i want to add flashlight Blinking mode in android studio with a button. but i don't know that how can i put a code and how to implement this code with a button. because i want that if i press button then flashlight start blinking.

can anyone tell me that how can i implement this code with a button?

String[] list1 = { "1", "0", "1", "0", "1", "0", "1", "0", "1", "0" };
    for (int i = 0; i < list1.length; i++) {
        if (list1[i].equals("0")) {
            params.setFlashMode(Parameters.FLASH_MODE_ON);
        } else {
            params.setFlashMode(Parameters.FLASH_MODE_OFF);
        }

    }

2 Answers2

0

You can use this code for blink i make this as method :

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();
            }
        }
    }

and it will call like this :

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

Hope this will work for you and yeah make string long if you want blink long time then

Milind Vyas
  • 294
  • 5
  • 12
  • yes you can do it but Thread.sleep will freee the ui and then how can we stop flash bliking agian???by clicking the button etc – Mazhar Iqbal Apr 09 '21 at 07:25
0

All of these below codes put inside any where in your activity/fragment: Status, handler, Camera variables:

        boolean isOn[] = {false};              
        boolean[] isBlinking = {false};
        Handler handler = new Handler();
        CameraManager camManager = null;
        String cameraId = null;

Handle button click listener on your activity/fragment:

 camManager = (CameraManager) view.getContext().getSystemService(Context.CAMERA_SERVICE);

 buttonFlash.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(!isBlinking[0]){
                BlinkMode(view);
                isBlinking[0] = true; 
            } else {
                if(splashThread2 != null){
                    splashThread2.interrupt(); // stop blinking
                    OffFlash();
                }
                isBlinking[0] = false;
            }
        }
    });

Blinking method:

    Thread splashThread2;
    private void BlinkMode(View view){
        splashThread2 = new Thread() {
            @Override
            public void run() {
                try {
                    while (!isInterrupted()) {
                        Thread.sleep(100);

                        if(!isOn[0]){
                            OnFlash();
                            isOn[0] = true;
                        } else {
                            isOn[0] = false;
                            OffFlash();
                        }

                    }
                } catch (Exception e) {
                }
            }
        };
        splashThread2.start();
    }

Handle when goback

 @Override
    public void onStop() {
        super.onStop();
        if(splashThread2 != null){
            splashThread2.interrupt();   // stop blinking
            OffFlash();
        }
        // todo check null exception
    }

    @Override
    public void onPause() {
        super.onPause();
        if(splashThread2 != null){
            splashThread2.interrupt();  // stop blinking
            OffFlash();
        }
        // todo check null exception
    }

On/Off Method for new version of Android:

    private void OnFlash(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            try {
                cameraId = camManager.getCameraIdList()[0];
                camManager.setTorchMode(cameraId, true);   //Turn ON
                isOn[0] = true;
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }
    }

    private void OffFlash(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            try {
                cameraId = camManager.getCameraIdList()[0];
                camManager.setTorchMode(cameraId, false);   //Turn ON
                isOn[0] = false;
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }
    }
Alias
  • 39
  • 1
  • 4