androidx / androidx/media

Starting DownloadService at 5AM in the background every day

Open
#1,239 6 comments 0 reactions 1 assignee View on GitHub

@marcbaechinger is already working on this.

Since Apr 3, 2024.

question
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 call DownloadService.sendAddDownload with the DownloadRequest for 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 call DownloadManager.addDownload directly 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.