How to make android phone flashlight blink?

前端 未结 3 389
盖世英雄少女心
盖世英雄少女心 2021-01-25 05:11

I am trying to make the LED flashlight of android phone blink based on binary code like if char = 1 turn LED light on else if char = 0 turn LED off.

if ( char ==         


        
3条回答
  •  长情又很酷
    2021-01-25 06:01

    use this method it works, im using in my app

    private void blink(final int delay, final int times) {
            Thread t = new Thread() {
                public void run() {
                    try {
    
                        for (int i=0; i < times*2; i++) {
                            if (isFlashOn) {
                                turnOffFlash();
                            } else {
                                turnOnFlash();
                            }
                            sleep(delay);
                        }
    
                    } catch (Exception e){ 
                        e.printStackTrace(); 
                    }
                }
            };
            t.start();
            }
    
         private void turnOnFlash() {
            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 turnOffFlash() {
            if (isFlashOn) {
                if (camera == null || params == null) {
                    return;
                }
                params = camera.getParameters();
                params.setFlashMode(Parameters.FLASH_MODE_OFF);
                camera.setParameters(params);
                camera.stopPreview();
                isFlashOn = false;
            }
        }
    

提交回复
热议问题