The solution from what I have searched is to use AlarmManager to keep it awake
That will not help. Once the device falls asleep, your socket connection will be terminated. You would need to use a partial WakeLock
plus a WifiLock
to keep the device powered on continuously.
But it is said that this will drain the battery of the device.
The WakeLock
and WifiLock
will definitely drain the battery.
So, is there another way to do this?
Not if you need to use WiFi.
For eg, what do apps like Whatsapp and Skype do?
They do not use WiFi when the device wants to go to sleep. Once the WiFi radio powers down, they use mobile data, so no WifiLock
is needed. For mobile data, incoming packets will wake up the device, so you only need a WakeLock
while you are actually doing work, rather than constantly.
The best answer is to switch to use C2DM, though.