使用java如何找出一个应用程序有一段时间没有使用?使用java如何找出一个应用程序有一段时间没有使用?
我有一些第三方应用程序,例如我们可以采取'Skype'。如果没有任何行动(鼠标/键盘输入)给Skype的5分钟,然后我的代码通过弹出一些用户。那么如何检查应用程序是否获得用户的输入?我经历了互联网,我发现下面的代码是给出输出,如果整个桌面空闲。但我需要特定的应用程序,如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) {}
}
}
}
“不使用”是什么意思?没有收到键盘或鼠标输入?没有线程在后台工作?你必须更具体。 – 2014-10-29 09:04:05
这听起来像是您正在寻找的特定于平台的特性,而Java仅支持可跨平台实现的功能。如果在Windows上可以使用此功能,那么您可能需要编写一个本地接口封装器或调用一些外部命令。 – 2014-10-29 09:04:41
只要应用程序执行某些操作,您就可以简单地将日期保存在某个地方。之后,您只需要将当前日期与上次使用日期相减即可。 – 2014-10-29 09:25:29