Handle custom URL schemes in an OS X Java application

后端 未结 3 1104
我在风中等你
我在风中等你 2020-12-16 07:21

The Info.plist of our Java-based application contains following entries:




        
3条回答
  •  有刺的猬
    2020-12-16 08:09

    In case anyone wanted a version using com.apple.eawt.* This also uses reflection, so it will compile on any platform (Windows etc.). Make sure not to call the method registering the event handler on any non-Apple system ;)

    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    import java.net.URI;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    interface OpenUriAppleEventHandler {
        public void handleURI(URI uri);
    }
    
    class OpenURIEventInvocationHandler implements InvocationHandler {
    
        private OpenUriAppleEventHandler urlHandler;
    
        public OpenURIEventInvocationHandler(OpenUriAppleEventHandler urlHandler) {
            this.urlHandler = urlHandler;
        }
    
        @SuppressWarnings({ "rawtypes", "unchecked"})
        public Object invoke(Object proxy, Method method, Object[] args) {
            if (method.getName().equals("openURI")) {
                try {
                    Class openURIEventClass = Class.forName("com.apple.eawt.AppEvent$OpenURIEvent");
                    Method getURLMethod = openURIEventClass.getMethod("getURI");
                    //arg[0] should be an instance of OpenURIEvent
                    URI uri =  (URI)getURLMethod.invoke(args[0]);
                    urlHandler.handleURI(uri);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            return null;
        }
    }
    
    public class OSXAppleEventHelper {
        /**
         * Call only on OS X
         */
        @SuppressWarnings({ "unchecked", "rawtypes" })
        public static void setOpenURIAppleEventHandler(OpenUriAppleEventHandler urlHandler) {
            try {
                Class applicationClass = Class.forName("com.apple.eawt.Application");
                Method getApplicationMethod = applicationClass.getDeclaredMethod("getApplication", (Class[])null);
                Object application = getApplicationMethod.invoke(null, (Object[])null);
    
                Class openURIHandlerClass = Class.forName("com.apple.eawt.OpenURIHandler", false, applicationClass.getClassLoader());
                Method setOpenURIHandlerMethod = applicationClass.getMethod("setOpenURIHandler", openURIHandlerClass);
    
                OpenURIEventInvocationHandler handler = new OpenURIEventInvocationHandler(urlHandler);
                Object openURIEvent = Proxy.newProxyInstance(openURIHandlerClass.getClassLoader(), new Class[] { openURIHandlerClass }, handler);
                setOpenURIHandlerMethod.invoke(application, openURIEvent);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
    

    Use it like this:

    //if(isOSX){
    OSXAppleEventHelper.setOpenURIAppleEventHandler(new OpenUriAppleEventHandler() {
    
        @Override
        public void handleURI(URI url) {
            /* do something with the url */
        }
    });
    

提交回复
热议问题