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

前端 未结 2 699
无人共我
无人共我 2020-12-31 05:32

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 th

相关标签:
2条回答
  • 2020-12-31 06:09

    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(); 
    
    0 讨论(0)
  • 2020-12-31 06:35

    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]).

    0 讨论(0)
提交回复
热议问题