当前位置: 首页 > wzjs >正文

深圳网站制作公司深圳app开发新浪nba最新消息

深圳网站制作公司深圳app开发,新浪nba最新消息,wordpress ini主题,Wordpress多重筛选插件使用Java调用Windows通知并显示自定义图标 一 实现原理,java调用shell脚本,实现弹出通知,其实是有两种shell脚本的方式,任何编程语言都可以调用shell的,底部附源码 在现代应用程序中,通知系统是一个非常重…

使用Java调用Windows通知并显示自定义图标

一 实现原理,java调用shell脚本,实现弹出通知,其实是有两种shell脚本的方式,任何编程语言都可以调用shell的,底部附源码

在现代应用程序中,通知系统是一个非常重要的功能,它可以帮助用户及时了解应用程序的状态或重要信息。本文将详细介绍如何使用Java调用Windows通知,并在通知中显示自定义图标。我们将通过一个完整的代码示例来展示这一功能的实现。

代码功能概述

本文的代码实现了一个Java方法
showWindowsNotificationWithIconA,它可以在Windows系统上显示一个带有自定义图标的通知。通知的标题和内容可以通过参数传递,图标则从项目的资源文件中加载。如果图标无法直接从资源文件中加载,代码还会尝试将图标提取到临时文件中,并使用该临时文件的路径来显示图标。

此外,代码还包含一个辅助方法 executeShellScriptEx,用于执行PowerShell脚本。该方法会将脚本保存到临时文件中,然后通过 ProcessBuilder 启动PowerShell进程来执行脚本,并捕获脚本的输出。

代码实现逻辑

    1. 加载图标资源

首先,代码尝试从项目的资源文件中加载图标。图标文件位于 resources/static/A.ico 路径下。通过
GlobalKeyListener.class.getResource(“/static/A.ico”) 方法获取图标的URL。

URL iconUrl = GlobalKeyListener.class.getResource("/static/A.ico");
if (iconUrl == null) {System.err.println("无法加载图标资源!");return;
}

如果图标资源无法加载,代码会输出错误信息并返回。

    1. 处理图标路径
      获取到图标的URL后,代码将其转换为适合PowerShell脚本使用的字符串形式,并进行必要的转义。如果URL不包含 file 协议(即图标不是从文件系统中加载的),代码会调用 extractIconToTemp 方法将图标提取到临时文件中。
String file = iconUrl.toString().replace("\\", "/");
if(!file.contains("file")){try {file = extractIconToTemp();} catch (Exception e) {throw new RuntimeException(e);}
}
    1. 构建PowerShell脚本

接下来,代码构建一个PowerShell脚本来显示通知。脚本使用了Windows的Toast通知系统,并指定了带图标的模板
ToastImageAndText02。通知的标题、内容和图标路径通过格式化字符串插入到脚本中。

String script = String.format("""$ErrorActionPreference = 'Stop';Add-Type -AssemblyName System.Runtime.WindowsRuntime;[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null;$template = [Windows.UI.Notifications.ToastTemplateType]::ToastImageAndText02;  # 使用带图标的模板$toastContent = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent($template);$toastXml = [xml] $toastContent.GetXml();$toastXml.SelectSingleNode('//text[1]').InnerText = '%s';  # 标题$toastXml.SelectSingleNode('//text[2]').InnerText = '%s';  # 内容$toastXml.SelectSingleNode('//image').Attributes['src'].Value = '%s';  # 图标路径$toastNode = [Windows.Data.Xml.Dom.XmlDocument]::new();$toastNode.LoadXml($toastXml.OuterXml);$toast = [Windows.UI.Notifications.ToastNotification]::new($toastNode);$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier('柚柚通知');  # 应用名称$notifier.Show($toast);""",title.replace("'", "''"), message.replace("'", "''"), file);
    1. 执行PowerShell脚本

最后,代码通过 WindowsSystemUtil.executeShellScriptEx
方法执行构建好的PowerShell脚本,从而在Windows系统上显示通知。

WindowsSystemUtil.executeShellScriptEx(script);
5. 提取图标到临时文件
如果图标无法直接从资源文件中加载,代码会调用 extractIconToTemp 方法将图标提取到临时文件中。该方法首先通过 ClassLoader 加载图标资源,然后将其写入一个临时文件,并返回该临时文件的绝对路径。

public static String extractIconToTemp() throws IOException {InputStream inputStream = GlobalKeyListener.class.getClassLoader().getResourceAsStream("static/A.ico");if (inputStream == null) {throw new FileNotFoundException("图标文件未找到!");}try {File tempFile = File.createTempFile("appicon", ".ico");tempFile.deleteOnExit(); // 程序正常退出时删除临时文件try (OutputStream outputStream = new FileOutputStream(tempFile)) {byte[] buffer = new byte[1024];int bytesRead;while ((bytesRead = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, bytesRead);}}return tempFile.getAbsolutePath();} finally {inputStream.close();}
}

完整代码

public static void showWindowsNotificationWithIconA(String title, String message)  {// 获取图标文件的URL。这里假设图标位于项目的resources/static/A.ico。URL iconUrl = GlobalKeyListener.class.getResource("/static/A.ico");if (iconUrl == null) {System.err.println("无法加载图标资源!");return;}// 将URL转换为适合PowerShell脚本使用的字符串形式,并进行必要的转义String file = iconUrl.toString().replace("\\", "/");if(!file.contains("file")){try {file = extractIconToTemp();} catch (Exception e) {throw new RuntimeException(e);}}// 构建PowerShell脚本String script = String.format("""$ErrorActionPreference = 'Stop';Add-Type -AssemblyName System.Runtime.WindowsRuntime;[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null;$template = [Windows.UI.Notifications.ToastTemplateType]::ToastImageAndText02;  # 使用带图标的模板$toastContent = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent($template);$toastXml = [xml] $toastContent.GetXml();$toastXml.SelectSingleNode('//text[1]').InnerText = '%s';  # 标题$toastXml.SelectSingleNode('//text[2]').InnerText = '%s';  # 内容$toastXml.SelectSingleNode('//image').Attributes['src'].Value = '%s';  # 图标路径$toastNode = [Windows.Data.Xml.Dom.XmlDocument]::new();$toastNode.LoadXml($toastXml.OuterXml);$toast = [Windows.UI.Notifications.ToastNotification]::new($toastNode);$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier('柚柚通知');  # 应用名称$notifier.Show($toast);""",title.replace("'", "''"), message.replace("'", "''"), file);// 打印构建好的脚本(可选)System.out.println(script);// 执行构建好的脚本WindowsSystemUtil.executeShellScriptEx(script);
}public static String extractIconToTemp() throws IOException {// 使用ClassLoader加载资源InputStream inputStream = GlobalKeyListener.class.getClassLoader().getResourceAsStream("static/A.ico");if (inputStream == null) {throw new FileNotFoundException("图标文件未找到!");}try {// 创建临时文件File tempFile = File.createTempFile("appicon", ".ico");tempFile.deleteOnExit(); // 程序正常退出时删除临时文件// 将输入流的数据写入临时文件try (OutputStream outputStream = new FileOutputStream(tempFile)) {byte[] buffer = new byte[1024];int bytesRead;while ((bytesRead = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, bytesRead);}}// 返回临时文件的绝对路径return tempFile.getAbsolutePath();} finally {inputStream.close();}
}

总结

通过本文的代码示例,我们展示了如何使用Java调用Windows通知并显示自定义图标。代码通过加载资源文件、处理图标路径、构建PowerShell脚本以及执行脚本等步骤,实现了这一功能。希望本文能帮助你更好地理解如何在Java应用程序中集成Windows通知功能。

http://www.dtcms.com/wzjs/313820.html

相关文章:

  • 北京朝阳社会建设工作办公室网站付费内容网站
  • 福田区网站建seo综合查询怎么用
  • 石家庄手机网站制作多少钱企业推广公司
  • 大众点评网怎么做团购网站推广软件哪个好
  • 餐饮加盟网站怎么做推广效果最好的平台
  • 网站怎做网络营销师有前途吗
  • app软件开发公司推荐湛江seo
  • 专业集团门户网站建设费用小程序开发文档
  • 网站页面设计代码seo快速排名首页
  • 百度网站安全检测平台天猫代运营
  • 做网站要实名吗广告推广平台网站有哪些
  • .org做商业网站运营是做什么的
  • 给传销产品做网站nba哈登最新消息
  • 广州网站制作托管做引流的公司是正规的吗
  • 做外汇上什么网站看新闻搜索引擎最新排名
  • 住房和城乡建设部网站 城市绿地分类免费的短视频app大全
  • 用国外服务器做赌博网站搜索引擎优化的流程是什么
  • 网站建设设计维片武汉seo技术
  • 门户网站html武汉seo广告推广
  • 天津做美缝的网站seo整站优化一年价格多少
  • 怎么制作爆米花教程seo专业培训学费多少钱
  • 网站备案号中信息有变营销排名seo
  • 宁波做网站建设五种新型营销方式
  • 定制化网站开发报价2021年热门关键词
  • 最新网站推广推蛙网络
  • 济宁做网站公司北京网络优化
  • 如何用服务器建设网站石家庄关键词优化软件
  • 郑州网站优化服务济南百度竞价代运营
  • 视频网站的服务器多大网站推广优化网址
  • 东城免费做网站北京seo优化排名推广