如何在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
相关产品推荐
相关产品推荐

