Is it possible to know the state of Flash light in Android. I want to run a loop, before which I would like to know whether the Flash Light of the device is in ON/OFF state.
Asked
Active
Viewed 1,089 times
0
-
Please find this LInk.. I hope it will clear all your doubt. [Link to Handle Flash Light](https://stackoverflow.com/questions/6068803/how-to-turn-on-camera-flash-light-programmatically-in-android) – coder_baba Dec 11 '17 at 10:05
-
@Shylesh try [this](https://stackoverflow.com/questions/6068803/how-to-turn-on-camera-flash-light-programmatically-in-android).it might help you.. – Harshad Prajapati Dec 11 '17 at 10:05
-
@HarshadPrajapati the above link helps the user to **Turn On and Off** the Flash. I want my code to **Check the current state** of the Flash. For instatnce, I Switch on the flash light from the notification bar shortcut. Then I open my app and press on **TEST** button, It Should show **"ON"** status. – Shylesh Dec 11 '17 at 10:13
2 Answers
3
Register the torchCallback
Handler handler = new Handler(getMainLooper());
CameraManager mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
CameraManager.TorchCallback torchCallback = new CameraManager.TorchCallback() {
@Override
public void onTorchModeChanged(String cameraId, boolean enabled){
super.onTorchModeChanged(cameraId, enabled);
boolean currentTorchState = enabled;
}
};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
mCameraManager.registerTorchCallback(torchCallback, handler);
}

Bùi Đức Khánh
- 3,975
- 6
- 27
- 43

Changho Paeon
- 31
- 2
-2
I think you can not get the STATE of Flash Light.
For that you have to manage the state manually like,
boolean isFlashStateOn = false;
.
.
@Override
public void onClick(View v) {
if (isFlashStateOn ) {
turnOffFlashLight();
isFlashStateOn = false;
} else {
turnOnFlashLight();
isFlashStateOn = true;
}
}

Dhruv Patel
- 1,529
- 1
- 18
- 27