在Java应用程序中检测Windows注销事件 [英] Detect windows logout event in Java application

查看:273
本文介绍了在Java应用程序中检测Windows注销事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有可以使用Java来查看用户注销和其他Windows事件的库? (更好的,如果它支持多个平台!)



我记得很多年前读过这样的图书馆,但现在似乎找不到。我已经看到其他线程使用Python与win32ts做基本上相同的事情。



如果它是免费的和/或开源的,也会更好。

$ b $



注意:使用 Runtime.getRuntime()的候选解决方案.addShutdownHook(Thread) javaw 中无法正常工作。我仍然在寻找一个可以与javaw一起使用的解决方案。请参阅java bug ids 4486580 4302814 。谢谢--cam

解决方案

我想你想看看 Runtime.addShutdownHook()。这给了你在JVM关闭之前做最后的挖掘机会。这样,您可以检测所有应用程序关闭的实际注销,但不能仅暂时切换用户,使其状态准备好重新启动。但是,作为一个警告,无论您在关机过程中打算做什么都需要简单,因为操作系统可能会随时对您进行SIGKILL(或等效),从而立即停止JVM。



如果这不是你需要的,你可能需要使用JNI来注册本机侦听器。对于其他Windows事件,AWT库提供了一些类来实现大多数普通应用程序感兴趣的听众。在这种情况下,您可能对 FocusEvent


Is there a library that I can use with Java to listen for user logout and possibly other Windows events? (Even better if it supports multiple platforms!)

I remember reading about a library of this sort a number of years ago, but can't seem to find it now. I've seen other threads to do essentially the same thing using Python with win32ts.

Also better if it's free and/or open source.

Thanks.

Note: The candidate solution of using Runtime.getRuntime().addShutdownHook(Thread) does not work correctly with javaw. I am still looking for a solution that will work with javaw. See java bug ids 4486580 and 4302814. Thanks --cam

解决方案

I think you want to look at Runtime.addShutdownHook(). This gives you the last-ditch opportunity to do things before the JVM shuts down. This lets you detect an actual logout where all of the applications are shutdown, but not they just temporarily switch users, leaving their state ready to come back to. However, as a warning, whatever you plan on doing during the shutdown process needs to be brief since the OS may SIGKILL (or the equivalent) on you at any time, which halts the JVM immediately.

If this isn't what you need, you're probably going to need to use JNI to register a native listener. As for the other windows events, the AWT library provides a number of classes for implementing the listeners that most normal apps would be interested in. In this case, you might be interested in a FocusEvent.

这篇关于在Java应用程序中检测Windows注销事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆