Click here to Skip to main content
15,893,668 members
Articles / Programming Languages / Java / Java SE

PC StandBy Detector for Java Apps

Rate me:
Please Sign up or sign in to vote.
3.63/5 (9 votes)
31 Mar 2004CPOL2 min read 65.4K   1.1K   20  
Description: Shows how to detect if the computer is going to go into sleep (stand-by) mode and allow/disallow it
package com.ha.common.windows;

/**
 * This class is used to detect is standby(sleep) was requested on the PC it is running on
 * and to disallow it optionally
 */

public class StandByDetector {
    static {
        System.loadLibrary("StandByDetector");
    }
    private StandByRequestListener listener;
    
    public StandByDetector(StandByRequestListener listener) {
        this.listener = listener;
        init();
    }
    
    public void fireStandByRequested() {
        listener.standByRequested();
    }
    
    private native boolean init();
  /*
     allowStandBy==false
     means that no standby is allowed while this app is running
   */
    public native void setAllowStandby(boolean allowStandby);
    public native void destroy();
    
    //for testing...
    public static void main(String args[]) {
        StandByDetector sd=new StandByDetector(new StandByRequestListener() {
            public void standByRequested() {
                System.out.println("standby requested");
            }
        });
        sd.setAllowStandby(false);
        javax.swing.JFrame f=new javax.swing.JFrame();
        f.getContentPane().add(new javax.swing.JLabel("close to end test"));
        f.setSize(300,100);
        f.setVisible(true);
        f.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions