You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Kotlin中访问Android Notification的when字段?

在Kotlin中访问Notification的when字段的解决方法

因为when是Kotlin的关键字,直接通过notification.when访问会触发语法错误,这里有两种可行的解决方式:

  • 使用反引号包裹关键字:把when用反引号括起来,让编译器将其识别为普通的字段名,而非关键字:
val notification: Notification = // 初始化你的Notification对象
val triggerTime = notification.`when`
  • 调用Java风格的getter方法:Notification类提供了getWhen()方法,Kotlin可以直接调用这个方法获取对应字段的值:
val notification: Notification = // 初始化你的Notification对象
val triggerTime = notification.getWhen()

两种方法都能正确获取到Notification中when字段存储的时间戳,可根据个人习惯选择使用。

内容的提问来源于stack exchange,提问作者KiYugadgeter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 12:20:54