使用java如何找出一个应用程序有时不使用?

时间:2014-10-29 09:01:19

标签: java windows desktop

使用java如何查找应用程序有时不使用?

我有一些第三方应用程序,例如我们可以使用'Skype'。如果没有动作(鼠标/键盘输入)给skype 5min,那么我的代码通过一些弹出窗口给用户。那么如何检查应用程序是否从用户那里获得了输入?我通过互联网我发现下面的代码,如果整个桌面空闲,它将提供输出。但我需要特定的应用程序,如Skype。怎么做?

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.sun.jna.*;
import com.sun.jna.win32.*;

/**
* Utility method to retrieve the idle time on Windows and sample code to test it.
* JNA shall be present in your classpath for this to work (and compile).
* @author ochafik
*/
public class Win32IdleTime {

public interface Kernel32 extends StdCallLibrary {
Kernel32 INSTANCE = (Kernel32)Native.loadLibrary("kernel32", Kernel32.class);

/**
* Retrieves the number of milliseconds that have elapsed since the system was started.
* @see http://msdn2.microsoft.com/en-us/library/ms724408.aspx
* @return number of milliseconds that have elapsed since the system was started.
*/
public int GetTickCount();
};

public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32)Native.loadLibrary("user32", User32.class);

/**
* Contains the time of the last input.
* @see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputstructures/lastinputinfo.asp
*/
public static class LASTINPUTINFO extends Structure {
public int cbSize = 8;

/// Tick count of when the last input event was received.
public int dwTime;
}

/**
* Retrieves the time of the last input event.
* @see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/getlastinputinfo.asp
* @return time of the last input event, in milliseconds
*/
public boolean GetLastInputInfo(LASTINPUTINFO result);
};

/**
* Get the amount of milliseconds that have elapsed since the last input event
* (mouse or keyboard)
* @return idle time in milliseconds
*/
public static int getIdleTimeMillisWin32() {
User32.LASTINPUTINFO lastInputInfo = new User32.LASTINPUTINFO();
User32.INSTANCE.GetLastInputInfo(lastInputInfo);
return Kernel32.INSTANCE.GetTickCount() - lastInputInfo.dwTime;
}

enum State {
UNKNOWN, ONLINE, IDLE, AWAY
};

public static void main(String[] args) {
if (!System.getProperty("os.name").contains("Windows")) {
System.err.println("ERROR: Only implemented on Windows");
System.exit(1);
}
State state = State.UNKNOWN;
DateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");

for (;;) {
int idleSec = getIdleTimeMillisWin32() / 1000;

State newState =
idleSec < 30 ? State.ONLINE :
idleSec > 5 * 60 ? State.AWAY : State.IDLE;

if (newState != state) {
state = newState;
System.out.println(dateFormat.format(new Date()) + " # " + state);
}
try { Thread.sleep(1000); } catch (Exception ex) {}
}
}

}

2 个答案:

答案 0 :(得分:2)

我认为没有用你的意思是,你作为用户不使用它活跃。比你可以使用WindowListener。这是一个示例工作代码(希望这是你想要的):

public class AwayTimer {
    private JFrame mainframe;  // the mainframe in which everything will we
    private JLabel timeLabel;  // our label which stores the time when were leaving the window

    private long leaveTime = 0; // that time is set to zero, to prevent it later saying strange numbers

    public static void main(String[] args) {
        new AwayTimer(); // create new AwayTimer object
    }

    public AwayTimer() {
        mainframe = new JFrame("Away Timer"); // create new Frame with name Away Timer

        timeLabel = new JLabel ("You were 0 seconds away.", SwingConstants.CENTER); // create new label which shows the time we were away

        WindowListener listener = new WindowListener() {
            @Override
            public void windowDeactivated(WindowEvent e) { // called on leaving focus
                leaveTime = System.currentTimeMillis(); // get the time when leaving the window and save it leaveTime
            }

            @Override
            public void windowActivated(WindowEvent e) { // called on switching to Frame
                // That also gets activated when we open the program. It shows you were <ahugenumber> seconds away. To set it 0, we check if leaveTime is 0 as we initialized it
                if (leaveTime == 0) return; // we dont need to calculate anything and leave the text as it is
                long difference = (System.currentTimeMillis() - leaveTime) / 1000; // calculate the difference between the leave time and now and divide it by 1000 to get the time in seconds
                timeLabel.setText("You were " + difference + " seconds away."); // change the displayed text
            }

            // Other listeners, which arent important for that
            @Override
            public void windowOpened(WindowEvent e) {/* do something here*/}

            @Override
            public void windowIconified(WindowEvent e) {/* do something here*/}

            @Override
            public void windowDeiconified(WindowEvent e) {/* do something here*/}

            @Override
            public void windowClosing(WindowEvent e) {/* do something here*/}

            @Override
            public void windowClosed(WindowEvent e) {/* do something here*/}
        };

        mainframe.addWindowListener(listener); // add the previously created listener to the window

        // add the label to the frame
        mainframe.add(timeLabel);
        mainframe.pack(); // resize the frame, so it fits the contents
        mainframe.setVisible(true);     // make it visible
    }
}

希望我能提供帮助。

答案 1 :(得分:1)

您可以在应用程序中实现MouseListner和KeyListener,然后您可以检查它。但是,如果您的应用程序正在处理我们需要思考的问题。请解释一下你的申请是做什么的?