I am currently developing an android app that gets data from a web server at a user specified interval (5 min, 10 min...). I am using an AlarmManager and a WakeLock. The alarm goes off as expected every 5-10 minutes. The internet connection though doesn't seem to be working during sleep. Most people suggest that I use a WifiLock. Correct me if I am wrong but isn't WifiLock only used to keep WiFi alive? What about 3G-4G mobile data? Does WifiLock keep that connection alive aswell?
-
1Have you try using services? Services behave independently and can be executed even if the app is closed. – UmarZaii Jul 17 '17 at 21:26
-
This has nothing to do with my problem. My code is executed as expected. However I do not have internet access. I try to download a file for example but it doesnt work. – Josh Lee Jul 17 '17 at 21:30
-
Check this out on how to turn on Internet even when the phone is in sleep mode.. [link](https://stackoverflow.com/questions/11926640/turn-on-the-internet-on-android-when-in-sleep) – UmarZaii Jul 17 '17 at 21:47
2 Answers
Starting with Android 6 due to the new Doze Mode the device enters sleep even with wakelocks, they are ignored.
The way to avoid the device to enter sleep is to start a foreground Service with a non-dismissable notification.

- 3,976
- 2
- 13
- 23
-
"The way to avoid the device to enter sleep" But I don't want to avoid sleep. I want my code to execute during sleep. Which is already doing. It just doesn't have access to the internet because the wifi / network shut down during sleep. – Josh Lee Jul 17 '17 at 21:40
-
so can you have your broadcast receiver start a foreground Service that does the data transfer? Would this wake the device which could then go back to sleep after the foreground service exited? – Dale Wilson Jul 17 '17 at 21:44
-
During sleep the phone stops totally, cpu stops execution, and when Brodacast Receiver handles an Alarm the cpu only is active until onReceive returns. – from56 Jul 17 '17 at 21:47
-
No, the service should be started previously and should be active during the time alarms are fired. – from56 Jul 17 '17 at 21:49
-
Then have it switch into and back out of foreground mode. [ Oh and it does not go back to sleep just because onReceive returns. It goes back to sleep because there's nothing to keep it awake.] – Dale Wilson Jul 18 '17 at 14:07
You can't get network access while the device is in Doze mode. If you are hoping to receive notifications or updates from a web service, use Firebase Cloud Messaging, or just defer the network call until the next time it wakes up. You can see more information about Doze restrictions here:
https://developer.android.com/training/monitoring-device-state/doze-standby.html#restrictions
Also, if you are curious about how to check if phone is in Doze mode:
https://developer.android.com/reference/android/os/PowerManager.html#isDeviceIdleMode()

- 2,144
- 1
- 17
- 36
-
I also tried implementing a foreground service. In the "onCreate" method I issue the wakelock and the wifilock. When the device sleeps, all the network requests my app (foreground service) makes timeout. Also, I tried running my app on a <6.0 device and this still occurs. So I believe this has nothing to do with Doze mode. – Josh Lee Jul 18 '17 at 00:49
-
Post your manifest and the service you are using to make this http call. Also, did you try running the request while holding onto a wakelock? – Pablo Baxter Jul 18 '17 at 16:23