How to use CameraX in foreground service?

1,582 views
Skip to first unread message

Kursad Laçin

unread,
Sep 14, 2021, 6:35:43 PM9/14/21
to Android CameraX Discussion Group
I need to use the Camera frames even the app is in background. After I have searched how to do it I encountered to foreground service. However, when the app is in background, the streaming from the Camera stops, so I cannot use the advantage of using foreground service.

    Camera                                       State                 
    -------------------------------------------------------------------
    Camera@dbfb30d[id=0]                         UNKNOWN               
    Camera@61d8d28[id=2]                         UNKNOWN               
    Camera@d99af2f[id=1]                         CLOSED

Whenever I touch the home button, the warning above comes to Log-cat and stop delivering frames to ImageAnalysis. I cannot process the frames.

All the CameraX dependencies is in a class which inherit from Service.

ps : manifest has been edited as android:foregroundServiceType="camera"

Eino-Ville Talvala

unread,
Sep 14, 2021, 7:08:43 PM9/14/21
to Kursad Laçin, Android CameraX Discussion Group
Hi Kursad -

Are you using a https://developer.android.com/reference/androidx/lifecycle/LifecycleService with ProcessCameraProvider#bindToLifecycle ?  That said, I'm not sure if foreground service usage is officially supported or not right now.


--
You received this message because you are subscribed to the Google Groups "Android CameraX Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to camerax-develop...@android.com.
To view this discussion on the web visit https://groups.google.com/a/android.com/d/msgid/camerax-developers/0f47ab0c-2ba6-4601-89eb-bc66fd5ebf34n%40android.com.

Kursad Laçin

unread,
Sep 15, 2021, 7:42:07 AM9/15/21
to Android CameraX Discussion Group, Eino-Ville Talvala, Android CameraX Discussion Group, Kursad Laçin
Hi again, 
I was getting the context from the main activity and it was working with Foreground service until I press to home button. After I implemented LifecycleOwner to the service, I cannot get the Camera frames even the app is open.

Here is my code: can you investigate and tell me what is wrong?


There is no error, however these are from the Logcat. 

D/CameraRepository: Added camera: 0
I/Camera2CameraInfo: Device Level: INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED
I/CameraManagerGlobal: postSingleUpdate device: camera id 0 status STATUS_PRESENT
I/CameraManagerGlobal: postSingleUpdate device: camera id 1 status STATUS_PRESENT
D/UseCaseAttachState: Active and attached use case: [] for camera: 0
I/CameraManagerGlobal: postSingleUpdate device: camera id 2 status STATUS_PRESENT
D/CameraRepository: Added camera: 1
I/Camera2CameraInfo: Device Level: INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED
I/CameraManagerGlobal: postSingleUpdate device: camera id 0 status STATUS_PRESENT
I/CameraManagerGlobal: postSingleUpdate device: camera id 1 status STATUS_PRESENT
I/CameraManagerGlobal: postSingleUpdate device: camera id 2 status STATUS_PRESENT
D/CameraRepository: Added camera: 2
D/UseCaseAttachState: Active and attached use case: [] for camera: 1
I/Camera2CameraInfo: Device Level: INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED
I/CameraManagerGlobal: postSingleUpdate device: camera id 0 status STATUS_PRESENT
I/CameraManagerGlobal: postSingleUpdate device: camera id 1 status STATUS_PRESENT
I/CameraManagerGlobal: postSingleUpdate device: camera id 2 status STATUS_PRESENT
D/UseCaseAttachState: Active and attached use case: [] for camera: 2
D/CameraOrientationUtil: getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=270, isOppositeFacing=false, result=270
D/DeferrableSurface: Surface created[total_surfaces=1, used_surfaces=0](androidx.camera.core.impl.ImmediateSurface@2797ba5}
D/Camera2CameraImpl: {Camera@50536c5[id=1]} Use case ImageAnalysis:androidx.camera.core.ImageAnalysis-bea87099-87f4-434d-9ba7-90571f6c862f ACTIVE

Zoran Smilevski

unread,
Sep 17, 2021, 2:51:26 AM9/17/21
to Android CameraX Discussion Group, mkrsa...@gmail.com, Eino-Ville Talvala, Android CameraX Discussion Group
It should work. Try to switch to LifecycleService ( implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"). Maybe your implementation is missing something.

Kursad Laçin

unread,
Sep 17, 2021, 6:23:37 AM9/17/21
to Android CameraX Discussion Group, zoran.s...@gmail.com, Kursad Laçin, Eino-Ville Talvala, Android CameraX Discussion Group
Thanks for your advice. I was using (" extends Service implements LifecycleOwner" ) before. I changed it to ("extends LifecycleService"). So it totally worked. 
Thank you very much!!

Reply all
Reply to author
Forward
0 new messages