小编use*_*543的帖子

如何在 Java 中执行特定于操作系统的通知?

我不是一名职业程序员(我所拥有的任何 Java 知识都来自 Hard Knocks 学院)。请原谅我提出的愚蠢问题,并妥善回答。

我正在开发的一个 Java 应用程序使用了非常有缺陷的与平台无关的通知(例如文件已成功下载时)。我想使用平台感知通​​知。在 Linux 上发出通知的代码非常简单:

import org.gnome.gtk.Gtk;
import org.gnome.notify.Notify;
import org.gnome.notify.Notification;

public class HelloWorld
{
    public static void main(String[] args) {
        Gtk.init(args);
        Notify.init("Hello world");
        Notification Hello = new Notification("Hello world!", "This is an example notification.", "dialog-information");
        Hello.show();
    }
}
Run Code Online (Sandbox Code Playgroud)

在 Mac 上,它有点复杂,但仍然可行:

interface NsUserNotificationsBridge extends Library {
    NsUserNotificationsBridge instance = (NsUserNotificationsBridge)
            Native.loadLibrary("/usr/local/lib/NsUserNotificationsBridge.dylib", NsUserNotificationsBridge.class);

    public int sendNotification(String title, String subtitle, String text, int timeoffset);
}
Run Code Online (Sandbox Code Playgroud)

它需要从这个 github 存储库获取 dylib:https://github.com/petesh/OSxNotificationCenter

Windows方式是这样的:

import java.awt.*;
import java.awt.TrayIcon.MessageType; …
Run Code Online (Sandbox Code Playgroud)

java notifications systray platform-specific

4
推荐指数
1
解决办法
1410
查看次数

标签 统计

java ×1

notifications ×1

platform-specific ×1

systray ×1