Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep CPU from 'sleeping' when screen is turned off in Android?

I have an application in which I am sending network data over WiFI. Everything is fine until I turn the display off or the device goes to 'sleep'. I'm already locking the WiFi however, it seems to be the case that the CPU speed ramps down when in sleep which causes my streaming to not behave properly (i.e. packets don't flow as fast as I would like as they do when the device is not sleeping).

I know that I possibly can/possibly should address this at the protocol level however, that might possibly not be possible as well...

Is there any way to "prevent the CPU from going to 'sleep' when the screen is off"? If so, how? If not, any advice on how to keep the speed of my WiFi stream consistent whether the device is in sleep mode or not?

like image 985
2 revs Avatar asked Oct 01 '11 01:10

2 revs


People also ask

How do I turn off auto sleep on my Android?

To enable/disable Auto-sleep and/or Battery saver functions: tap the Sync icon in the upper left hand corner of the screen. go to Settings – Battery saver/Auto-sleep.


2 Answers

Grab a PARTIAL_WAKE_LOCK from the PowerManager. You'll also need to add the WAKE_LOCK permission to your manifest.

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Tag"); 
wl.acquire();
//do what you need to do
wl.release(); 
like image 149
Michael Madsen Avatar answered Sep 22 '22 01:09

Michael Madsen


Okay, so, after much more research and experimenting, it seems that the real issue is the fact that, at least on some phones, their WiFi goes into a 'partial sleep' mode EVEN IF you've taken the WiFi lock. It seems that this is what the 'WIFI_MODE_FULL_HIGH_PERF' flag was invented for when taking the WiFi lock... unfortunately, this flag is only available on some devices/Android versions (I have no clue as to which but, it wasn't available to me). So, therefore, it isn't a fix for all devices.

The only "solution" (which is actually a kludge) seems to be to 'detect when the screen is turned off and then, set an alarm that turns the screen back on immediately thereafter'. The links that helped a little bit with this are:

How to keep a task alive after phone sleeps?

and

http://android.modaco.com/topic/330272-screen-off-wifi-off/

I hope that this helps people who are experiencing WiFi disruption when the phone goes to sleep/screen is turned off (and the phone is unplugged/disconnected [e.g. you won't see this effect when connected to adb; only when the phone is running with nothing connected to it]).

like image 29
2 revs Avatar answered Sep 21 '22 01:09

2 revs