Java 你如何让机器保持清醒?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you keep the machine awake?
提问by Frank Krueger
I have a piece of server-ish software written in Java to run on Windows and OS X. (It is not running on a server, but just a normal user's PC - something like a torrent client.) I would like the software to signal to the OS to keep the machine awake (prevent it from going into sleep mode) while it is active.
我有一个用 Java 编写的服务器软件,可以在 Windows 和 OS X 上运行。(它不在服务器上运行,而只是普通用户的 PC - 类似于 torrent 客户端。)我希望该软件发出信号到操作系统以在机器处于活动状态时保持机器处于唤醒状态(防止其进入睡眠模式)。
Of course I don't expect there to be a cross platform solution, but I would love to have some very minimal C programs/scripts that my app can spawn to inform the OS to stay awake.
当然,我不希望有跨平台解决方案,但我希望有一些非常小的 C 程序/脚本,我的应用程序可以生成这些程序/脚本来通知操作系统保持清醒。
Any ideas?
有任何想法吗?
采纳答案by ScArcher2
I use this code to keep my workstation from locking. It's currently only set to move the mouse once every minute, you could easily adjust it though.
我使用此代码来防止我的工作站锁定。它目前仅设置为每分钟移动一次鼠标,不过您可以轻松调整它。
It's a hack, not an elegant solution.
这是一个黑客,而不是一个优雅的解决方案。
import java.awt.*;
import java.util.*;
public class Hal{
public static void main(String[] args) throws Exception{
Robot hal = new Robot();
Random random = new Random();
while(true){
hal.delay(1000 * 60);
int x = random.nextInt() % 640;
int y = random.nextInt() % 480;
hal.mouseMove(x,y);
}
}
}
回答by Gulzar Nazim
Run a command inside a timer like pinging the server..
在计时器内运行命令,例如 ping 服务器。
回答by zigdon
Wouldn't it be easier to disable the power management on the server? It might be argued that servers shouldn't go into powersave mode?
在服务器上禁用电源管理不是更容易吗?可能有人会说服务器不应该进入省电模式?
回答by Keng
I have a very brute-force technique of moving the mouse 1 point in the x direction and then back every 3 minutes.
我有一种非常强力的技术,可以将鼠标在 x 方向上移动 1 个点,然后每 3 分钟返回一次。
There may me a more elegant solution but it's a quick fix.
我可能有一个更优雅的解决方案,但它是一个快速解决方案。
回答by Matt Dillard
On Windows, use the SystemParametersInfofunction. It's a Swiss army-style function that lets you get/set all sorts of system settings.
在 Windows 上,使用SystemParametersInfo函数。这是一个瑞士军队风格的功能,可让您获取/设置各种系统设置。
To disable the screen shutting off, for instance:
要禁用屏幕关闭,例如:
SystemParametersInfo( SPI_SETPOWEROFFACTIVE, 0, NULL, 0 );
Just be sure to set it back when you're done...
完成后请务必将其设置回...
回答by Paulj
I'd just do a function (or download a freebie app) that moves the mouse around. Inelegant, but easy.
我只是做一个移动鼠标的功能(或下载一个免费的应用程序)。不优雅,但很容易。
回答by zigdon
Wouldn't all the suggestions moving the mouse back and forth drive the user crazy? I know I'd remove any app that would do that as soon as I can isolate it.
所有来回移动鼠标的建议不会让用户发疯吗?我知道我会尽快删除任何会这样做的应用程序。
回答by David Crow
I've been using pmsetto control sleep mode on my Mac for awhile now, and it's pretty easy to integrate. Here's a rough example of how you could call that program from Java to disable/enable sleep mode. Note that you need root privileges to run pmset, and therefore you'll need them to run this program.
一段时间以来,我一直在使用pmset来控制 Mac 上的睡眠模式,并且它很容易集成。这是一个粗略的示例,说明如何从 Java 调用该程序以禁用/启用睡眠模式。请注意,您需要 root 权限才能运行 pmset,因此您需要它们来运行此程序。
import java.io.BufferedInputStream;
import java.io.IOException;
/**
* Disable sleep mode (record current setting beforehand), and re-enable sleep
* mode. Works with Mac OS X using the "pmset" command.
*/
public class SleepSwitch {
private int sleepTime = -1;
public void disableSleep() throws IOException {
if (sleepTime != -1) {
// sleep time is already recorded, assume sleep is disabled
return;
}
// query pmset for the current setting
Process proc = Runtime.getRuntime().exec("pmset -g");
BufferedInputStream is = new BufferedInputStream(proc.getInputStream());
StringBuffer output = new StringBuffer();
int c;
while ((c = is.read()) != -1) {
output.append((char) c);
}
is.close();
// parse the current setting and store the sleep time
String outString = output.toString();
String setting = outString.substring(outString.indexOf(" sleep\t")).trim();
setting = setting.substring(7, setting.indexOf(" ")).trim();
sleepTime = Integer.parseInt(setting);
// set the sleep time to zero (disable sleep)
Runtime.getRuntime().exec("pmset sleep 0");
}
public void enableSleep() throws IOException {
if (sleepTime == -1) {
// sleep time is not recorded, assume sleep is enabled
return;
}
// set the sleep time to the previously stored value
Runtime.getRuntime().exec("pmset sleep " + sleepTime);
// reset the stored sleep time
sleepTime = -1;
}
}
回答by Milhous
回答by asloob
Adding to scarcher2's code snippet above and moving mouse by only 1 pixel. I have moved the mouse twice so that some change occurs even if pointer is on extremes:
添加到上面的 Scarcher2 代码片段并将鼠标移动仅 1 个像素。我移动了鼠标两次,以便即使指针在极端情况下也会发生一些变化:
while(true){
hal.delay(1000 * 30);
Point pObj = MouseInfo.getPointerInfo().getLocation();
System.out.println(pObj.toString() + "x>>" + pObj.x + " y>>" + pObj.y);
hal.mouseMove(pObj.x + 1, pObj.y + 1);
hal.mouseMove(pObj.x - 1, pObj.y - 1);
pObj = MouseInfo.getPointerInfo().getLocation();
System.out.println(pObj.toString() + "x>>" + pObj.x + " y>>" + pObj.y);
}