Application Inactivity
Posted by Rob Camick on October 24, 2008
There are times when you may want to monitor your application for inactivity. Maybe you want to log out a user after a certain time interval. This requirement can be broken down into two main steps. We will need to:
- listen for events at the application level
- track the time interval between these events
Whenever the time interval between events exceeds our inactivity threshhold we will then invoke the inactivity Action.
Luckily for us there is an easy solution to both steps. First, we can use an AWTEventListener to listen for events. This listener can be added to the Toolkit class to listen for events generated by the AWT system. We also have the ability to control the events we want to listen for. Secondly, we can use the Swing Timer to control the inactivity interval. The timer is setup to invoke an inactivity Action after a specified interval. However, when the AWTEventListener receives an event it can simply tell the timer to reset itself. So as long as events are received the timer will never fire.
The functionality of these two classes has been combined into the InactivityListener class. The following information is required to use the class:
- the Action to invoke (when the Timer fires)
- the inactivity interval
- an event mask used by the AWTEventListener
You can create many different event masks to be used by the AWTEVentListener. However, I have provided a few common masks that you are likely to use:
- KEY_EVENTS
- MOUSE_EVENTS (includes MouseMotionEvents)
- USER_EVENTS (combines KEY_EVENTS and MOUSE_EVENTS), this is the default
So, to schedule an automatic logout from your application after 10 minutes of no keyboard or mouse activity you would do something like:
JFrame frame = new JFrame(...);
...
frame.setVisible( true );
Action logout = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
JFrame frame = (JFrame)e.getSource();
frame.dispose();
}
};
InactivityListener listener = new InactivityListener(frame, logout, 10);
listener.start();
Get The Code
Related Reading
How to Use Swing Timers
Java API: java.awt.ToolKit.addAWTEventListener(…)
Java API: java.awt.AWTEvent
Anonymous said
The article and the given program is very useful. I am very thankful to you for writing the same. Thanks a lot. Please keep writing such useful articles and such programs.
Rob Camick said
Thanks for the feedback.
Anonymous said
can you show me a sample of implementation of Action logout = new YourLogoutAction(…);? thanks
Anonymous said
Hi,
This works only for your application but not capture whole windows inactivity. I mean mouse over or key press on your application is captured but not the one done on windows or any other application. How do I know if user is doing any activity on machine and not necessarily on my application.
Rob Camick said
That is correct, this only works for your Java application. I don’t know how to access Windows events. Java would not be the best language to do this.
Anonymous said
This is a very useful program indeed. This is exactly what I need. I’m going to use your code. :) Thanks so much!!!…
Anonymous said
Useful Info….!!!
Anonymous said
Useful! I will be using it.
Anonymous said
Thank you a lot for very helpful topic. this is idea that I am searching.
Thanks
Anonymous said
Many thanks for this topic. After much searching I come across this and in 1 page you provided an answer of what I needed. Thanks
Anonymous said
Thank you. Even after 8 years, you still help someone with your posts.
Ashutosh Tiwari said
Hi Rob Camick, this is very useful articles, Can I also catch the timer progress so that I can also add message in GUI, dialog is about to close
Randika Perera said
Thank you very much! Your article saved me form lot of hassles.
Rob Camick said
Glad the suggestions helped.
Anonymous said
Super, thanks a lot.