Java – How do I get a Java application to detect that a Linux PC has resumed from a suspended state (sleep mode)?

How do I get a Java application to detect that a Linux PC has resumed from a suspended state (sleep mode)?… here is a solution to the problem.

How do I get a Java application to detect that a Linux PC has resumed from a suspended state (sleep mode)?

How does

a Java Swing desktop application running on Linux (such as Ubuntu) notify the application that the PC has just resumed from sleep/pause/hibernate state?

Screen blanking is not a problem. All other pending or hibernate states (suspended to RAM or suspended to disk) are worth paying attention to (for my purposes they will be treated equally).

My goal is to automatically call class methods when the PC or device is restored. (This method requires a restart of rxtxSerial, fwiw.) )

Edit: Thorbjørn Ravn Andersen’s comment made me realize that my question was not clear. My goal is to listen for operating system events that indicate that the system has just resumed from a suspended state (or utilize system log files).

Solution

Have the daemon thread read the system time, say once per second. If there is a (obvious) gap between the two readings, your system has just woken up. However, it is uncertain whether this will prevent the system from going to sleep in the first place.

Related Problems and Solutions