androidkotlinandroid-camera2flashlightandroid-thread

Torch: How to register a torch callback in another thread?


I am trying to register a CameraManager.TorchCallback using CameraManager.registerTorchCallback into a specific thread (ie: different from the UI Thread).

My code is quite basic for an Camera2 implementation:

  1. Get the CameraManager
  2. Get the camera id from the manager
  3. Register my CameraManager.TorchCallback using CameraManager.registerTorchCallback
  4. Update the Torch status using the CameraManager

If I run that code in the main thread, everything is working great, when I run it into another thread class (overriding run), the callback is never called. I am ensuring that my thread is not terminated, visible on the Android studio profiler. There is something with the registration and the threading I don't understand.

Have you ever tried that kind of implementation?

Any idea is welcome!


Solution

  • Even if you call registerTorchCallback() from a background thread, the callback will still be posted to UI thread. You must create a new HandlerThread and get a Handler for it.