Starting DownloadService at 5AM in the background every day
@marcbaechinger is already working on this.
Since Apr 3, 2024.
- Dominant language
- Java
- Stars
- 3k
- Forks
- 955
- Avg merge
- 12d 14h
- Merged PRs (30d)
- 2
Description
Hi, we use media3 library to display video and play audio in our application to take a user through a workout routine step by step. We would like to give the user an option to predownload some of this media every morning (while their phone is charging and they have a network), so that the media is available regardless of their internet connection later in the day.
I have tried the following:
- Create a CoroutineWorker that gets waken up every morning at 5AM by the alarm manager.
- In the worker, use
setForegroundAsync(foregroundInfo), and then callDownloadService.sendAddDownloadwith theDownloadRequestfor all media files.
However, that doesn't work, since API > 31 doesn't allow starting services from the background, since ForegroundServiceStartNotAllowedException gets thrown.
I have also tried the following:
- Create a CoroutineWorker that gets waken up every morning at 5AM by the alarm manager.
- In the worker, use
setForegroundAsync(foregroundInfo), and then callDownloadManager.addDownloaddirectly to schedule the download.
However, this doesn't seem to work correctly either, and there every once in a while there is an ANR that is recorded in the FirebaseCrashlytics that originates from the worker, so I'm not convinced that that is a right approach either.
Any ideas how to approach this? Everything that I have found originates from user pressing the button in the app, but what are recommendations if we want to offer the user the ability to pre-cache all of their media for a smoother offline experience. Thank you !
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.