具体如何使用Notification.Builder
我发现我正在使用已弃用的通知方法(notification.setLatestEventInfo()),
它说要使用Notification.Builder。
- 如何使用它?
当我尝试创建新实例时,它会告诉我:
Notification.Builder cannot be resolved to a type
I found that I am using a deprecated method for noficitations (notification.setLatestEventInfo())
It says to use Notification.Builder.
- How do I use it?
When I try to create a new instance, it tells me:
Notification.Builder cannot be resolved to a type
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
更新 android-N(2016 年 3 月)
请访问通知更新 链接了解更多详细信息。
。示例代码。
UPDATE android-N (march-2016)
Please visit Notifications Updates link for more details.
sample code.
我在构建通知时遇到问题(仅针对 Android 4.0+ 进行开发)。
此链接准确地向我展示了我做错了什么,并说以下:
基本上我错过了其中之一。作为解决此问题的基础,请确保您至少拥有所有这些。希望这能帮其他人省去一些麻烦。
I was having a problem building notifications (only developing for Android 4.0+).
This link showed me exactly what I was doing wrong and says the following:
Basically I was missing one of these. Just as a basis for troubleshooting with this, make sure you have all of these at the very least. Hopefully this will save someone else a headache.
即使在 API 8 中也能正常工作
你可以使用这个代码:
或者我建议遵循一个优秀的 关于此的教程
It works even in API 8
you can use this code:
Or I suggest to follow an excellent tutorial about this
我用过
I have used
万一它对任何人有帮助......在针对较新的旧 API 进行测试时,我在使用支持包设置通知方面遇到了很多麻烦。我能够让它们在较新的设备上工作,但会在旧设备上进行错误测试。
最终让它对我有用的是删除与通知功能相关的所有导入。特别是NotificationCompat 和TaskStackBuilder。看来,在开始设置我的代码时,导入是从较新的版本添加的,而不是从支持包添加的。然后当我稍后想在 eclipse 中实现这些项目时,系统就不再提示我再次导入它们。希望这是有道理的,并且它可以帮助其他人:)
In case it helps anyone... I was having a lot of trouble with setting up notifications using the support package when testing against newer an older API's. I was able to get them to work on the newer device but would get an error testing on the old device.
What finally got it working for me was to delete all the imports related to the notification functions. In particular the NotificationCompat and the TaskStackBuilder. It seems that while setting up my code in the beginning the imports where added from the newer build and not from the support package. Then when I wanted to implement these items later in eclipse, I wasn't prompted to import them again. Hope that makes sense, and that it helps someone else out :)
独立示例
与此答案中的技术相同,但是:
来源:
在 Android 22 中测试。
Self-contained example
Same technique as in this answer but:
Source:
Tested in Android 22.
Notification.Builder API 11 或 NotificationCompat.Builder API 1
这是一个使用示例。
Notification.Builder API 11 or NotificationCompat.Builder API 1
This is a usage example.
此位于 API 11 中,因此如果您正在开发任何内容3.0 之前的版本您应该继续使用旧的 API。
更新:NotificationCompat.Builder 类已添加到支持包中,因此我们可以使用它来支持 API 级别 v4 及更高版本:
http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html
This is in API 11, so if you are developing for anything earlier than 3.0 you should continue to use the old API.
Update: the NotificationCompat.Builder class has been added to the Support Package so we can use this to support API level v4 and up:
http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html
除了所选答案之外,这里还有来自 来源技巧:
in addition to the selected answer here is some sample code for the
NotificationCompat.Builder
class from Source Tricks :通知生成器严格适用于 Android API Level 11 及更高版本(Android 3.0 及更高版本)。
因此,如果您的目标不是 Honeycomb 平板电脑,则不应使用通知生成器,而应遵循旧的通知创建方法,如下所示 示例。
Notification Builder is strictly for Android API Level 11 and above (Android 3.0 and up).
Hence, if you are not targeting Honeycomb tablets, you should not be using the Notification Builder but rather follow older notification creation methods like the following example.