【问题标题】:Create windows 10 persistent notifications创建 Windows 10 持久通知
【发布时间】:2017-01-28 03:35:18
【问题描述】:

我已使用this answer 在 Windows 10 操作中心成功创建了一个通知弹出窗口。问题是,通知会在那里停留 5 秒钟,然后一旦消失,就会从操作中心完全删除。如何让操作中心保留通知,直到用户关闭它?代码如下:

import java.awt.*;
import java.awt.TrayIcon.MessageType;

import javax.swing.JOptionPane;

public class Win10Notif {

    public static void main(String[] args) throws AWTException, java.net.MalformedURLException {

        if (SystemTray.isSupported()) {
            Win10Notif td = new Win10Notif();
            td.displayTray();
        } else {
            System.err.println("System tray not supported!");
        }
    }


    public void displayTray() throws AWTException, java.net.MalformedURLException {
        //Obtain only one instance of the SystemTray object
        SystemTray tray = SystemTray.getSystemTray();

        //If the icon is a file
        Image image = Toolkit.getDefaultToolkit().createImage("icon.png");
        //Alternative (if the icon is on the classpath):
        //Image image = Toolkit.getToolkit().createImage(getClass().getResource("icon.png"));
        TrayIcon trayIcon = new TrayIcon(image, "Tray Demo");
        //Let the system resizes the image if needed
        trayIcon.setImageAutoSize(true);
        //Set tooltip text for the tray icon
        trayIcon.setToolTip("System tray icon demo");
        tray.add(trayIcon);
        trayIcon.displayMessage("Hello, World", "notification demo", MessageType.INFO);
    }
}

【问题讨论】:

    标签: java windows windows-10


    【解决方案1】:

    我认为这是由 Windows 本身或 JVM 的本机实现管理的。至少,公共 API 不提供在屏幕上为通知设置特定时间的选项。

    除非您需要坚持操作中心,否则您可以考虑使用外部库进行桌面通知,如下所示:

    • JCarrierPigeon: 很小,很快;尽管它依赖于 Timing Framework 库。就连它带来的 API 也很小。
    • JTelegraphJCarrierPigeon 的扩展,带有一些现成的图标和样式。当然,这个也依赖于 Timing Framework 库。
    • JCommunique:最全的选择之一,这意味着更大的足迹;但至少这个没有依赖关系,而且非常灵活,涵盖了很多用例场景。
    • Twinkle:它很时尚,但不是那么轻巧。包括开箱即用的图标、动画和其他资源。该代码在编译时有一些依赖项,但我认为可分发的 .jar 已经捆绑了所有内容。它可免费用于非商业目的。
    • DS Desktop Notify:它体积小、重量轻、易于设置且没有依赖关系。它可以以与JOptionpane.showMessageDialog() 相同的方式使用,或者通过在手动发布通知对象之前构建和自定义通知对象。可以自定义颜色主题、图标、屏幕上的时间和操作等属性,还提供股票主题和图标。

    您可以免费获取并试用其中的任何一个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      相关资源
      最近更新 更多