How to check if the flashlight is on?

2020-07-26 10:57发布

问题:

Is there any way to check once you have tried to turn the flashlight on if it has worked or not?

I'm using:

p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();

To turn the camera on. This works with most phones, but not with all phones so I need to know if it's worked or not.

Edit: Just to be clear, I need to know if turning the flash on has actually worked or not. Sometimes you need to use FLASH_MODE_ON instead of FLASH_MODE_TRUE, and sometimes you need to use FLASH_MODE_ON even when

List<String> flashModes = p.getSupportedFlashModes();
flashModes.contains(Parameters.FLASH_MODE_TORCH)

returns true.

Thanks for any help.

回答1:

First you need to check if the device supports flashlight:

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

Then you can safely assume that it will be turned on when you start preview.



回答2:

Works for me:

camera.getParameters().getFlashMode().equals(Parameters.FLASH_MODE_TORCH)

Returns true flashlight is on

Note: Camera class has been deprecated in API level 21. so this solution won't work for recent versions