0

If a phone is setup to turn off the WiFi when the phone goes into sleep mode, my app will lose it's connection. It is a well documented problem that when some devices come out of sleep mode, the WiFi connection doesn't even reconnect although the WiFi icon indicates that it is connected. I have seen this happen on one of my devices.

But I am at a loss as to how the Gmail app works. Even in sleep mode, if mail comes in, you get notified. Yet if the WiFi is disabled in sleep mode, then the only way the Gmail app can check for mail is to turn on the WiFi. I assume it turns it on and then back off after it is done checking for mail. So how can I accomplish the same thing? Or am I wrong and the WiFi never gets turned off because Gmail always keeps it on? But how would I know that?

Johann
  • 27,536
  • 39
  • 165
  • 279
  • hi @AndroidDev, I'm facing same issue now. do you resolve this issue? if you found any solution for this issue please help me.. – user512 Apr 01 '16 at 13:53

2 Answers2

0

Because it doesn't rely on WiFi, a data connection over the mobile network will do. Or are you saying you have disabled mobile network data?

Gmail and others use push notifications by the way, see Cloud to Device Messaging (C2DM) if you are curious.

Update: Also note that even if setting the wifi sleep mode policy to off when screen is off it doesn't usually turn off the wifi directly but waits a bit. I think it varies by phone model, but around 5-15min.

Update2: Gmail (or the push channel) could also periodically turn on the wifi of course even if you have a wifi sleep mode policy set to off on standby. You can do this too of course but please be very careful about battery life if so. If you use push notifications this is handled for you and coordinated for power efficiency.

Mattias Isegran Bergander
  • 11,811
  • 2
  • 41
  • 49
  • Updated with wifi sleep policy settings details, could that be it? – Mattias Isegran Bergander May 06 '12 at 15:29
  • Is AlarmManager the way Gmail is temporarily waking up the WiFi? – Johann May 06 '12 at 15:43
  • I don't know if the Gmail-app is doing that, or if it even cares at all or just let the push data channel handle that. But AlarmManager is certainly a good way of periodically doing something. Again, if you do that be careful with the battery levels of our phones. It is a sure way of getting bad reviews if it drains the battery. Another way could be to piggy back on others waking the wifi, listen for network connection: http://stackoverflow.com/questions/1783117/network-listener-android – Mattias Isegran Bergander May 06 '12 at 15:49
  • Regarding the link you posted. I tried that code earlier. It will report a network connection when coming out of sleep mode when there isn't one. Besides, I don't want to detect the network connectivity. I want to establish one. – Johann May 06 '12 at 15:54
  • It was just an idea to make it more power efficient as that IS a major concern. Rather in-efficient if 20 different apps all just use their own timer intervals to wake up and establish a connection instead of cooperating. But it is of course possible to acquire a wifi lock. A complete example is given here: http://goltermann.cc/2011/11/android-accessing-wifi-even-in-standby-using-wakelock-wifilock-alarmmanager-and-services/ – Mattias Isegran Bergander May 06 '12 at 16:02
0

Obtain a wifi lock, which will not be impacted by the user's wifi sleep policy- however, be sure to wait/delay/sleep before executing a function requiring wifi because the device will need time to actually connect to the network before it is useful. Release the wifi lock when done, but again, battery life will suffer. Users are pretty keen on wake locks/wifi locks, so add plenty of warnings.

gauglerb
  • 258
  • 1
  • 12
  • "Obtain a wifi lock, which will not be impacted by the user's wifi sleep policy" Is this documented sommewhere ? or just an observation ? – Ahmed Feb 14 '14 at 13:11
  • Observation... Ability to programmatically obtain a wifi lock will be granted by the android permission. – gauglerb Feb 18 '14 at 13:41