开发者

Logout when java app is inactive for 30 min problem

I need to implement logout when java desktop app is ( when there is no clicking buttons for 30 min ) inactive for 30 min. How to implent this ? Does anybody have any idea开发者_JS百科 ? Is there any observer already for this in Java ?


There's actually two parts to this:

  1. detect inactivity
  2. log out the user

For the first part, this mostly depends on how you define "inactive". One option: Have some time field for "last acitivy", which gets updated by whatever code implements an acitivy. You could use a java.util.Timer that fires once a minute and checks if "last activity" is more than 30 minutes ago, then logs the user out.

Another option is to use the approach from this question to implement a resettable timer, which you could then reset whenever there is activity. Then the logout would be after exactly 30 min, not 30-31 minutes.

As to "logging out the user", this will depend on your OS. Java itself does not have an API for this, but on most platforms you can just invoke an OS binary using Runtime.exec or similar. Also note that logging out the user may require elevated privileges on some platforms, so take this into account.

Finally, maybe you could explain what the underlying problem is? Why can't you for example just set the OS screensaver to lock the screen after 30 minutes? Wouldn't that be easier?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜