我的应用程序定期在屏幕上显示信息。但是如果屏幕截图处于活动状态,则应用程序应该等到用户返回。
有没有办法确定屏幕保护程序是否正在运行?
我不需要一个干净的解决方案,你只需要在windows xp上工作。
类似的问题,但其他技术: How to determine that a screensaver is running?
答案 0 :(得分:2)
嗯,这绝对不干净,但它可以作为一种肮脏的解决方法:
我检查“任何”屏幕保护程序(具有.SCR后缀)是否正在运行。
private static boolean isScreensaverRunning() {
List<String> p = WindowsUtils.listRunningProcesses();
for (String s : p) {
if (s.endsWith(".SCR")) {
return true;
}
}
return false;
}
public static List<String> listRunningProcesses() {
List<String> processes = new ArrayList<String>();
try {
String line;
Process p = Runtime.getRuntime().exec("tasklist.exe /fo csv /nh");
BufferedReader input = new BufferedReader
(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
if (!line.trim().equals("")) {
// keep only the process name
line = line.substring(1);
processes.add(line.substring(0, line.indexOf("\"")));
}
}
input.close();
}
catch (Exception err) {
err.printStackTrace();
}
return processes;
}
listRunningProcesses的来源:http://www.rgagnon.com/javadetails/java-0593.html
答案 1 :(得分:2)
尝试使用JNA库来调用SystemParametersInfo系统调用。
以下示例使用JNA提供的win32 examples代码:
public class JNATest {
public static void main(String[] args) {
W32API.UINT_PTR uiAction = new W32API.UINT_PTR(User32.SPI_GETSCREENSAVERRUNNING);
W32API.UINT_PTR uiParam = new W32API.UINT_PTR(0);
W32API.UINT_PTR fWinIni = new W32API.UINT_PTR(0);
PointerByReference pointer = new PointerByReference();
User32.INSTANCE.SystemParametersInfo(uiAction, uiParam, pointer, fWinIni);
boolean running = pointer.getPointer().getByte(0) == 1;
System.out.println("Screen saver running: "+running);
}
}
public interface User32 extends W32API {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class, DEFAULT_OPTIONS);
long SPI_GETSCREENSAVERRUNNING = 0x0072;
boolean SystemParametersInfo(
UINT_PTR uiAction,
UINT_PTR uiParam,
PointerByReference pvParam,
UINT_PTR fWinIni
);
}