How to detect that a PC has been idle for 30 seconds using Java?
Asked Answered
O

4

9

How to detect that a PC has been idle for 30 seconds using Java?

EDITED

With idle I mean that there are no user activity. The user does nothing in 30 seconds. I wold like to do an apllication like Windows, that detects that the user does nothing and enter in stad-by.

Octa answered 5/3, 2009 at 10:20 Comment(3)
can you rephrase your question in a more specific way?Conventioner
You need to define 'idle' - is it no user input, no disk activity, no processes running, an average CPU load or what?Swede
What does "idle" even mean? No/little CPU activitiy (what about multiproc systems)? No user input? No network traffic? No harddisk I/O?Fillian
H
6

Directly from java it is not possible. You'll have to find some native library and create a JNI wrapper for that.

Hinayana answered 5/3, 2009 at 10:42 Comment(0)
L
12

I've come across this excellent example of detecting system wide idle time using JNA. Java Native Access (JNA) is a library that provides access to Java Native Interface (JNI) without the need for you to actually write native code.

Here's the example that is very close to what you're asking for.

Lodhia answered 5/3, 2009 at 18:44 Comment(0)
D
7

Not possible as requested, but you can create workarounds using the java.lang.management API. A JNI implementation can also be found on javaworld.

Disseise answered 5/3, 2009 at 11:11 Comment(0)
H
6

Directly from java it is not possible. You'll have to find some native library and create a JNI wrapper for that.

Hinayana answered 5/3, 2009 at 10:42 Comment(0)
S
5

Not easy to do have a look at OperatingSystemMXBean method getSystemLoadAverage() only available since java 6 you could keep a track of it and if it is the same for a while determine the pc is idle.

This assumes idle in terms of idle CPU

OperatingSystemMXBean operatingSystemMxBean = ManagementFactory.getOperatingSystemMXBean();

Sardius answered 5/3, 2009 at 11:12 Comment(2)
It always returns -1 on Windows 7. Any hints?Leelah
According to the JavaDoc. If the load average is not available, a negative value is returned. You could be out of luck for this method working on Windows 7.Sardius

© 2022 - 2024 — McMap. All rights reserved.