Reputation: 1529
As per document, Google allows only below things as a background processes,
also,
While an app is in the foreground, it can create and run both foreground and background services freely. When an app goes into the background, it has a window of several minutes in which it is still allowed to create and use services. At the end of that window, the app is considered to be idle. At this time, the system stops the app's background services, just as if the app had called the services' Service.stopSelf() methods.
Under certain circumstances, a background app is placed on a temporary whitelist for several minutes. While an app is on the whitelist, it can launch services without limitation, and its background services are permitted to run. An app is placed on the whitelist when it handles a task that's visible to the user, such as:
My App (targetSdkVersion 26), which needs to be download a large file(~100 MB) in background (a state, app not exist in recent list even). I have create a Service
to achieve this, but as I am removing my app from recent, my download get stops. So, Google does really means, an app cannot execute a download process in background with targetSdkVersion 26?
Upvotes: 1
Views: 880
Reputation: 24937
Google does really means, an app cannot execute a download process in background with targetSdkVersion 26?
It imposes restriction on executing a Service in background. However you can still get your job done in background.
Approach 1:
If it really takes very long to download your necessary data, you can make use of new WorkManager
API.
As the documentation say, WorkManager is intended for tasks that require a guarantee that the system will run them even if the app exits, like uploading app data to a server. It is not intended for in-process background work that can safely be terminated if the app process goes away.
You can schedule a work which can be one time job or periodic. Moreover it also allows you to specify constraints, like internet connectivity required. This should be preferred for deferrable or asynchronous tasks.
Approach 2: Create ForegroundService
Alternatively, you can start a ForegroundService immediately and execute the task before terminating the Service. You might need to request for partial wake lock in some cases.
You can also look at my answer on this SO for more details.
Upvotes: 3