如何使用 Java 代码在 Ubuntu 中引发用户通知?

sek*_*har 8 java application-development

如何使用 Java 代码在 Ubuntu 中引发用户通知?

Set*_*eth 11

您可以使用java-gnomeGTK 和 GNOME 的 java 绑定来显示 notify-osd 通知。您需要先安装库:

sudo apt-get install libjava-gnome-java libjava-gnome-java-doc   
Run Code Online (Sandbox Code Playgroud)

这是一个快速示例:

sudo apt-get install libjava-gnome-java libjava-gnome-java-doc   
Run Code Online (Sandbox Code Playgroud)

通知的一般格式是这样的:

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

public class notifyTest {
    public static void main(String[] args) {  

        Gtk.init(args); // initialize Gtk
        Notify.init("Program Name"); // initalize the notification system  

        Notification myNotification = new Notification("Hello world!", "This is an example notification.", "dialog-information"); // create the notification object
        myNotification.show(); // show the notification  

    }


}
Run Code Online (Sandbox Code Playgroud)

无论是bodyicon字段可以为空,但必须有一个总结。有关默认情况下可以使用的图标列表,请查看Ubuntu Wiki的Notify-OSD 页面

然后你打电话:

Notification someName = new Notification("Summary", "Body", "Icon")  
Run Code Online (Sandbox Code Playgroud)

显示通知。有关更多信息,请参阅 java-gnome通知通知文档页面。

注意:您必须先初始化 Gtk 和 Notify,然后才能发送任何通知。