我如何制作一个持续通知,每次用户看到它都会更新? 形成服务
最佳答案
要在 Service
运行时显示通知,您可以调用:
startForeground(R.string.notification_id, myNotification);
为该方法提供您的服务的 ID 和您创建的 Notification
。
在任何时候,您的 Service
都可以通过使用相同的 R.string.notification_id
并发布新的 Notification
来更新用户看到的内容:
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(R.string.notification_id, myNotification);
要创建 Notification
,您需要阅读 Notification.Builder
( android docs here )。
相关问题也有很好的答案:How exactly to use Notification.Builder?很抱歉没有重新发布他的答案,但它包含很多代码,可以帮你解决问题。
关于android - 来自服务的持久性 android 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14320631/