Developing an application on Android. Created a service that constantly exchanges network packets with a server.

The problem is that when the phone “falls asleep” the packets begin to crumble. The connection is interrupted periodically due to a timeout.

I understand the mission of energy saving, but how can I get out of this situation?

  • one
  • one
    Thank you for your reply. But the fact is that when you don’t really know what to google, the search process is delayed. It’s much easier to "ask knowledgeable people" right away. And then Google will already help to understand the intricacies. - Konstantin Pl

1 answer 1

There are many nuances - it depends on whether the connection to the server is via WIFI or mobile. The exchange with the server is possible via a mobile connection but not via wifi when the device is in hibernation.

What are the ways out?

Of course, the easiest option is simply not to let the device fall asleep, but it will quickly devour the battery.

Another option is to make an exchange only when the devil wakes up. Also, the example can periodically wake up the device with the help of AlarmManager and do what you need.

Well, or look towards GCM . By the way there is a magic flag Delay_while_idle .

If the flag is not set to true, it’s still a flag. Otherwise, it will be stored in the GCM servers until the device is awake. It’s a bit like a flag of peace of mind. the old message will be collapsed by the new one).