问题描述
我想打开 MS Teams 以拨打带有 <a href="tel:.....
标签的 HTML 网页上的号码。
据我所知,Java 中的 HTML 版本很旧,可能包含的版本不支持 tel:
。但是,我尝试了这段代码,它对于普通的 HMTL 链接工作正常。
String st = "<html>\r\n<a href=\"tel:+4917312345678\">0173 12345678</a>\r\n</html>";
editorPane = new JEditorPane("text/html",st);
// handle link events
editorPane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED))
try {
Desktop.getDesktop().browse(e.getURL().toURI());
} catch (IOException e1) {
// Todo Auto-generated catch block
e1.printstacktrace();
} catch (URISyntaxException e1) {
// Todo Auto-generated catch block
e1.printstacktrace();
} // roll your own link launcher or use Desktop if J6+
}
});
editorPane.setEditable(false);
editorPane.setBounds(72,244,423,137);
panel_3.add(editorPane);
但这不适用于 tel:
链接
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.net.URL.toURI()" because the return value of "javax.swing.event.HyperlinkEvent.getURL()" is null
有什么想法吗?
解决方法
好的,我找到了一种使用 tel: 链接的方法。 我的代码仅适用于 Windows,因此如果您想使用跨平台,则必须先进行检查,例如 here
String st = "<html>\r\n<a href=\"tel:+4917312345678\">+4917312345678</a>\r\n</html>";
editorPane = new JEditorPane("text/html",st);
// handle link events
editorPane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED))
try {
Runtime rt = Runtime.getRuntime();
String url = e.getDescription();
System.out.println(url);
rt.exec("rundll32 url.dll,FileProtocolHandler " + url);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
editorPane.setEditable(false);
editorPane.setBounds(72,244,423,137);
panel_3.add(editorPane);