How to cancel shutdown in Mac OS X when application is an agent?

匿名 (未验证) 提交于 2019-12-03 01:03:01

问题:

I have a Java application that I need to hide from Dock and I also need to be able to interrupt. This is how I handle shutdown:

import com.apple.eawt.AppEvent.QuitEvent; import com.apple.eawt.QuitHandler; import com.apple.eawt.QuitResponse; import com.apple.eawt.Application;  public class MacOSXCustomizer {     public void init() {         Application application = Application.getApplication();         application.setQuitHandler(new QuitHandler() {             public void handleQuitRequestWith(QuitEvent qe, QuitResponse qr) {                 if(Main.prepareForExit()) {                     qr.performQuit();                 } else {                     qr.cancelQuit();                 }             }         });     } } 

I use my own launcher for Java and I use and Application bundle with my own Java launcher binary. I set LSUIElement to YES which solved my problem with a Dock icon, but then shutdown hook stopped working. My method is called during shutdown (I can log it) but application is terminated even if qr.cancelQuit() is called. It seems like a system is not waiting for response. Even if there is running some operations (2 seconds long) it is not finished. It causes data loss.

I tried to set LSUIElement back to NO and then system cancels shutdown when qr.cancelQuit() is called. No other change was done.

I also tried to create simple Cocoa application which implements only one method:

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {     return NSTerminateCancel; } 

it displays dialog "The application ShutdownTest canceled logout." but user session is logged out anyway.

My question is how can I solve to not have icon in Dock and Menu bar and to be able to cancel/interrupt shutdown sequence?

Edit: It is not relevant whether it is a Java application. Native applications have same behavior.

回答1:

I disscussed it with Apple Tech Support and my requested behavior is not possible. The application designed as an Agent (for instance by setting LSUIElement to YES) is not allowed to interrupt shutdown or logout sequence. System simply doesn't wait for response and don't bother to read and respect return value.

Background application (that does not have Dock icon and Menu bar) is not allowed to interact with user nor cancel his shutdown sequence.

Apple Tech Support says that there is no possibility to hide Dock icon and Menu bar and be foreground application.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!