in foreground service, check if any other app is using camera?

210 views Asked by At

I am using camera to take pictures in the foreground service, which can be used long term like 5 to 10 hours. The problem is when another app starts using camera while the foreground service is running it stop taking pictures and user have to manually restart the service.

I am taking pictures in a loop ( I initializes the camera instance takes a picture then releases the camera and repeat). The solution I see here, is that at the start of each loop iteration I should add a check to see if the camera is currently in use.

How exactly can I check if the camera is in use by any other application or process on device? or is there a better approach to my problem?

2

There are 2 answers

1
Ahmad Malik On

This worked for me!!

if returns true, means camera is free

public boolean isCameraFree() {
        Camera camera = null;
        try {
            camera = Camera.open();
        } catch (RuntimeException e) {
            return false;
        } finally {
            if (camera != null) camera.release();
        }
        return true;
    }
0
Wasim Ansari On

If you are using camera2 api, checkout CameraManager.AvailabilityCallback callback. it will inform you via callback when a cameraID is available/unavailable.