2014-10-29 26 views
-1

使用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) {} 
 
} 
 
} 
 

 
}

+0

“不使用”是什么意思?没有收到键盘或鼠标输入?没有线程在后台工作?你必须更具体。 – 2014-10-29 09:04:05

+0

这听起来像是您正在寻找的特定于平台的特性,而Java仅支持可跨平台实现的功能。如果在Windows上可以使用此功能,那么您可能需要编写一个本地接口封装器或调用一些外部命令。 – 2014-10-29 09:04:41

+1

只要应用程序执行某些操作,您就可以简单地将日期保存在某个地方。之后,您只需要将当前日期与上次使用日期相减即可。 – 2014-10-29 09:25:29

回答

1

您可以在应用程序中实现MouseListner和KeyListener的,然后你可以检查一下。但是如果你的应用程序正在处理一些我们还需要考虑的事情。请解释你的应用程序做了什么?

+0

@Frozn,完美答案!不需要实现MouseListener和KeyListener。超喜欢! – 2014-10-29 10:17:25

+0

但你的也是一种方法。 – Frozn 2014-10-30 09:47:57

+0

@Frozn,是的,但你的方式更加简单直接,我喜欢它! – 2014-10-30 10:49:48

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

谢谢你的帮助.. – Manikandan 2014-10-30 08:35:43

相关问题