简体   繁体   English

在Android中关闭屏幕时如何防止CPU“休眠”?

[英]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. 我有一个应用程序,我通过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 (ie packets don't flow as fast as I would like as they do when the device is not sleeping). 我已经锁定了WiFi,但似乎情况是在睡眠时CPU速度下降导致我的流不正常(即数据包不像我们想的那样快速流动设备没有睡觉)。

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"? 没有办法 “在屏幕关闭时防止CPU进入'睡眠'”? 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? 如果没有,关于如何保持WiFi流的速度的任何建议是否一致,无论设备是否处于睡眠模式?

Grab a PARTIAL_WAKE_LOCK from the PowerManager. 从PowerManager 获取PARTIAL_WAKE_LOCK You'll also need to add the WAKE_LOCK permission to your manifest. 您还需要向清单添加WAKE_LOCK权限。

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(); 

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. 好吧,经过更多的研究和实验,似乎真正的问题是,至少在某些手机上,他们的WiFi会进入“部分睡眠”模式, 即使您已经使用了WiFi锁定。 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). 看来这就是'WIFI_MODE_FULL_HIGH_PERF'标志是在发射WiFi锁时发明的......不幸的是,这个标志仅在某些设备/ Android版本上可用(我不知道哪个但是,它不可用对我来说)。 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'. 唯一的“解决方案”(实际上是一个kludge)似乎是'检测屏幕何时关闭,然后设置警报,此后立即重新打开屏幕'。 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/ 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 [eg you won't see this effect when connected to adb; only when the phone is running with nothing connected to it]). 我希望这可以帮助那些在手机进入睡眠状态/屏幕关闭时遇到WiFi中断的人(并且手机已拔下/断开连接[例如,当连接到adb时你不会看到这种效果; 只有当手机正在运行时)没有任何东西连接到它])。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM