androidrx-java2android-workmanagerbackground-serviceandroid-background

Need to run task in android background services?


In my app, I am using ffmpeg-kit and execute a command that record/capture any stream using the internet and writes it to the device's internal storage.
This command or stream can run until the user stops. And from here you can get an idea of this task that this execution can be 10 min, 30 mint or even 1 hr or more depending on the user till he wants to stop or the stream it self complete. e.g: video ends
Now my constraints are regarding this task that the app runs this FFmpeg command in the foreground and background but doesn't need to run when the app is killed by the user or system.
This task is only present and running while the app is running either in the foreground or background.

So now I need a suggestion from experts on which android services are best for use in this case. I run across a few of them... IntentService, JobScheduler and WorkManager

I've thought of using WorkManager here but my work is not persistent here and neither I need to run the FFmpeg and record stream while the app closed..so WorkManager is not suitable for this scenario.


Solution

  • None of those is the right answer. IntentService isn't a solution, as background services get killed in 2 minutes. JobScheduler would have the same problem- processing time limitations. As would Workmanager.

    What you want is a foreground service. Foreground services are services that can run for long times (they will still also be eventually killed, but can run for hours). Foreground services are launched by using startForegroundService instead of startService, and require the Service to call startForeground and provide a notification that will be in the status bar as long as the service is running.