I am developing an app in which i need to send and receive packets in background. But I’ve discovered that when screen shut off also cpu and networking is disabled. So I’ve found on Android Developers that I should use wake lock and wifi lock to let the cpu works altought if the screen went off. This solution works for other devices such LG Optimus One but not for Samsung Galaxy Tab. With this device when screen shut off I can’t receive no more packets (UDP) and magically when I press power button for re enabling screen, I restart to receive packets.
In my code I call:
powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_W AKE_LOCK, "TAG");
then
wakeLock.acquire();
I think that is a Samsung bug but I’m not sure of this, and if someone can give me some hint i would really appreciate.
P.S. I need some programming help… I already know that if I modify some manual setting to avoid shutting down the screen, this will work. But I’m developing an app and i need code!!
You should use
PARTIAL_WAKE_LOCKin your case.I’m not sure why
SCREEN_DIM_WAKE_LOCKis not enough (didn’t find any documentation that justifies this), butPARTIAL_WAKE_LOCKworked just fine for me in a similar scenario.As another option you may use
WifiLock. But you need to be smart about that, as your network connection may be 2G or 3G, not Wi-Fi only. So you shouldn’t hold Wi-Fi lock when you are actually using 3G/2G/etc.