Fra*_*ger 27 java windows macos operating-system cross-platform
我有一个用Java编写的服务器软件,可以在Windows和OS X上运行.(它不是在服务器上运行,而只是普通用户的PC - 类似于torrent客户端.)我希望软件发出信号在操作系统处于活动状态时保持机器处于唤醒状态(防止其进入睡眠模式).
当然我不希望有一个跨平台的解决方案,但我希望有一些非常小的C程序/脚本,我的应用程序可以产生,以通知操作系统保持清醒.
有任何想法吗?
ScA*_*er2 29
我使用此代码来防止我的工作站锁定.它目前只设置为每分钟移动一次鼠标,你可以很容易地调整它.
这是一个黑客,而不是一个优雅的解决方案.
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*ard 14
在Windows上,使用SystemParametersInfo函数.这是一种瑞士军队风格的功能,可以让你获得/设置各种系统设置.
要禁用屏幕关闭,例如:
SystemParametersInfo( SPI_SETPOWEROFFACTIVE, 0, NULL, 0 );
Run Code Online (Sandbox Code Playgroud)
你完成后一定要把它放回去......
一个更简洁的解决方案是使用 JNA 来利用本机操作系统 API。在运行时检查您的平台,如果它恰好是 Windows 那么以下将起作用:
import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.Structure.FieldOrder;
import com.sun.jna.platform.win32.WTypes.LPWSTR;
import com.sun.jna.platform.win32.WinBase;
import com.sun.jna.platform.win32.WinDef.DWORD;
import com.sun.jna.platform.win32.WinDef.ULONG;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.win32.StdCallLibrary;
/**
* Power management.
*
* @see <a href="/sf/answers/1469729481/">/sf/answers/1469729481/</a>
*/
public enum PowerManagement
{
INSTANCE;
@FieldOrder({"version", "flags", "simpleReasonString"})
public static class REASON_CONTEXT extends Structure
{
public static class ByReference extends REASON_CONTEXT implements Structure.ByReference
{
}
public ULONG version;
public DWORD flags;
public LPWSTR simpleReasonString;
}
private interface Kernel32 extends StdCallLibrary
{
HANDLE PowerCreateRequest(REASON_CONTEXT.ByReference context);
/**
* @param powerRequestHandle the handle returned by {@link #PowerCreateRequest(REASON_CONTEXT.ByReference)}
* @param requestType requestType is the ordinal value of {@link PowerRequestType}
* @return true on success
*/
boolean PowerSetRequest(HANDLE powerRequestHandle, int requestType);
/**
* @param powerRequestHandle the handle returned by {@link #PowerCreateRequest(REASON_CONTEXT.ByReference)}
* @param requestType requestType is the ordinal value of {@link PowerRequestType}
* @return true on success
*/
boolean PowerClearRequest(HANDLE powerRequestHandle, int requestType);
enum PowerRequestType
{
PowerRequestDisplayRequired,
PowerRequestSystemRequired,
PowerRequestAwayModeRequired,
PowerRequestMaximum
}
}
private final Kernel32 kernel32;
private HANDLE handle = null;
PowerManagement()
{
// Found in winnt.h
ULONG POWER_REQUEST_CONTEXT_VERSION = new ULONG(0);
DWORD POWER_REQUEST_CONTEXT_SIMPLE_STRING = new DWORD(0x1);
kernel32 = Native.load("kernel32", Kernel32.class);
REASON_CONTEXT.ByReference context = new REASON_CONTEXT.ByReference();
context.version = POWER_REQUEST_CONTEXT_VERSION;
context.flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING;
context.simpleReasonString = new LPWSTR("Your reason for changing the power setting");
handle = kernel32.PowerCreateRequest(context);
if (handle == WinBase.INVALID_HANDLE_VALUE)
throw new AssertionError(Native.getLastError());
}
/**
* Prevent the computer from going to sleep while the application is running.
*/
public void preventSleep()
{
if (!kernel32.PowerSetRequest(handle, Kernel32.PowerRequestType.PowerRequestSystemRequired.ordinal()))
throw new AssertionError("PowerSetRequest() failed");
}
/**
* Allow the computer to go to sleep.
*/
public void allowSleep()
{
if (!kernel32.PowerClearRequest(handle, Kernel32.PowerRequestType.PowerRequestSystemRequired.ordinal()))
throw new AssertionError("PowerClearRequest() failed");
}
}
Run Code Online (Sandbox Code Playgroud)
然后当用户运行时powercfg /requests
他们会看到:
SYSTEM:
[PROCESS] \Device\HarddiskVolume1\Users\Gili\.jdks\openjdk-15.0.2\bin\java.exe
Your reason for changing the power setting
Run Code Online (Sandbox Code Playgroud)
您应该能够对 macOS 和 Linux 执行类似的操作。
添加上面的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);
}
Run Code Online (Sandbox Code Playgroud)
所有来回移动鼠标的建议不会让用户发疯吗?我知道我会尽快删除任何会这样做的应用程序。
归档时间: |
|
查看次数: |
25106 次 |
最近记录: |