I am executing httpClient in AsyncTask but before this another AsyncTask is used to generate some data, which can take long time. If device gets sleep before executing httpClient, what would happen? Do I need wakeLock for httpClient requests?
Asked
Active
Viewed 1,057 times
4
-
I think the async task go also to sleep and wake up when the device wake up. If you want that your async task finish his work without a little rest you have to use wakeLock. – JackTools.Net Dec 26 '12 at 19:08
-
@JackTools.Net And why do you believe it? – Raffaele Dec 26 '12 at 19:09
-
How can a thread or an async task run when the cpu sleeps? – JackTools.Net Dec 26 '12 at 19:13
-
@JackTools.Net Please define *CPU sleeps* – Raffaele Dec 26 '12 at 19:16
-
@JackTools.Net I was wondering if device is sleeping (and so the asyncTask) then how and why am I getting debug messages ? – SohailAziz Dec 26 '12 at 19:18
-
The CPU stop to work. Look at http://developer.android.com/reference/android/os/PowerManager.html you need a partial_wake_lock to keep the cpu working. – JackTools.Net Dec 26 '12 at 19:19
-
@JackTools.Net I don't think they changed anything, because Android is a mobile OS, and without a running CPU it couldn't check for incoming calls and SMS ;) Don't confuse the power saving setting of a phone with the suspend/resume functionality in a laptop – Raffaele Dec 26 '12 at 19:36
-
Take a look at http://scape.cs.vt.edu/wp-content/uploads/2012/08/ITJ12_Android_Energy-Aware.pdf Chapter three – JackTools.Net Dec 26 '12 at 19:54
-
@JackTools.Net Interesting paper, but can't see the point where the CPU is shut down. Quote? – Raffaele Dec 26 '12 at 20:27
-
When no wave locks are detected, the operating system places the hardware in deep sleep to save energy [9] <- That is what I think too and this is what my app do without wake lock. – JackTools.Net Dec 26 '12 at 20:35
-
1Take a look at [this][1] page. It would be helpful. [1]: http://stackoverflow.com/questions/5120185/android-sleep-standby-mode – JackTools.Net Dec 26 '12 at 20:44
-
@JackTools.Net I see. Also found [this](https://groups.google.com/forum/?fromgroups=#!topic/android-developers/EMagdkxWvpU). The problem is the behavior is device-dependent and context-sensitive (power supply plugged in) - above all, the root is in the core clock, whose stop suspends most scheduling. But the OP question is related to an AsyncTask either CPU or IO bound, so, even after reading your resources, I don't think Android goes to deep sleep while it's computing something or it's waiting for IO – Raffaele Dec 27 '12 at 00:39
1 Answers
0
If the device go sleep, your activity onPause()
is called. If you don't cancel/terminate the background task in some fancy way, nothing happens to it. AsyncTask
are run in a separate thread than the main application one, so nothing can stop them. Ever.
I didn't understand your design: what's the purpose of two separate AsyncTask
s? If gathering data to fill the HTTP request consumes time, you should use a single AsyncTask
and do everything inside doInBackground()
, to ensure no race condition happens. Otherwise you'd have to chain tasks, or fire the second one in the first one, and this doesn't sound right.
The HTTP client is just a socket library, is not a mythological beast :)

Raffaele
- 20,627
- 6
- 47
- 86
-
Even if I use single AsyncTask if its long running would I need wakeLock? – SohailAziz Dec 26 '12 at 19:20
-
1You never need a wakeLock, if you use one, two, three, four, a hundred tasks. [Demo project](http://pastebin.com/kaSjucR1) which sleeps ten seconds and then logs a line. Launch this Activity (requires 4.0+) and see if the log line is printed while the device screen is turned off – Raffaele Dec 26 '12 at 19:26
-
My Logs are printed too but HttpClient not working. Working perfectly when used wakelock. – SohailAziz Dec 26 '12 at 20:04
-
@SohailAziz don't think it's a problem with WakeLock. Can you run the [following code](http://pastebin.com/TUPgFDQ2) (requires 4.0+ and INTERNET permission)? – Raffaele Dec 26 '12 at 20:23
-
@Raffaelle change your demo projekt to write a line to your log every minute and run it for an hour or two to test it. If you get 120 Loglines after two hours then I belive that you don't need wake_lock. – JackTools.Net Dec 26 '12 at 20:53