How to continue download task even when after the device is locked (pressing the power button). I tried installing an app from the play store, and during its download i locked the device, but it still continues to download without any interruption and is visible on the lockscreen as shown in the screen shot below. How do we achieve this programmatically?
Asked
Active
Viewed 1.1k times
3
-
use background threads, check out http://stackoverflow.com/a/7635918/5108318 – pzmarzly Jun 02 '16 at 20:20
-
I have a thread that downloads file via http inside a webview. The problem is that the download is interrupted if the device is locked. And the page returns a 401 when device is unlocked. I tried methods such as "keepScreenon" and setting wake lock with PARTIAL_WAKE_LOCK. But they don't get the expected result.. I observed what happens on playstore installation and it was interesting the download progress is visible even on the lock screen. So I would like to learn from what is implemented in this example. – Ganga Jun 02 '16 at 20:25
-
I think webView may hibernate when screen is off. Because you cannot force screen to stay on, the best option would be to write your own code to download – pzmarzly Jun 02 '16 at 20:37
-
try using DownloadManager - https://developer.android.com/reference/android/app/DownloadManager.html – pzmarzly Jun 02 '16 at 20:39
-
Yes the code to download is written, the webview just has a button that initiates the download on click. It is possible to keepscreen turned on without going to sleep using the `keepScreenOn` to `true` but they are not helpful in my case. It is not useful when we explicitly lock the device. – Ganga Jun 02 '16 at 20:41
2 Answers
0
If you want a task to continue running, even when your Activity is not active, you need to use a Service
. https://developer.android.com/guide/components/services.html. The Play Store is probably using a Service to keep the download running in the background, and a Notification to show the status on the lock screen. https://developer.android.com/guide/topics/ui/notifiers/notifications.html

chessdork
- 1,999
- 1
- 19
- 20
-
Thanks..Sorry I forgot to mention that the thread runs inside a service already. – Ganga Jun 02 '16 at 20:37
0
The example screenshot is misleading, since this is the Google Play Store.
Otherwise, one has to initiate the tranfers by DownloadManager
.

Martin Zeitler
- 1
- 19
- 155
- 216