【问题标题】:It is possible to change the Icon of the status bar (notification icon) dynamically?可以动态更改状态栏的图标(通知图标)吗?
【发布时间】:2011-01-21 11:57:34
【问题描述】:

我有一个安卓APP,有很多活动。

在我的应用程序的登录活动中,我在状态栏中启动了一个通知图标,它一直固定在那里,直到我的应用程序停止。好的,它有效。

但现在我还需要一件事,我需要通过我的应用程序的服务以编程方式动态地更改图标。我该怎么做?

如何访问我的应用程序的通知图标然后更改图标?

我希望通过代码示例来说明如何实现这一点。

【问题讨论】:

    标签: android notifications statusbar


    【解决方案1】:

    只需在NotificationManager 上再次调用notify(),并使用新的Notification,但使用与第一个相同的唯一ID。它将替换现有Notification 的图标(如果用户清除了第一个Notification,则显示新的Notification)。

    【讨论】:

    • 它不起作用,我正在这样做但通知栏没有更新,它仍然显示相同的文本和图标:S
    • @AndroidUser99:好的,然后取消()原来的通知并提出新的通知。
    • 如何取消该服务的原始通知?
    • @AndroidUser99:在NotificationManager 上调用cancel(),传入要删除的通知的唯一ID。
    • @AndroidUser99: 如果你notify() 已经存在Notification,它不会重新显示代码文本。如果你需要显示代码文本,你需要cancel()Notification,然后再notify()
    【解决方案2】:

    您可以在通知上使用 iconLevel: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#More

    在 res/drawable/myicon.xml 中创建一个具有不同级别(不同图标)的 xml 文件 http://developer.android.com/reference/android/graphics/drawable/LevelListDrawable.html

    <level-list xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:maxLevel="0" android:drawable="@drawable/ic_wifi_signal_1" />
      <item android:maxLevel="1" android:drawable="@drawable/ic_wifi_signal_2" />
      <item android:maxLevel="2" android:drawable="@drawable/ic_wifi_signal_3" />
    </level-list>
    

    并设置或(更新)级别:

    Notification mNotification = new Notification(icon, tickerText, when);
    mNotification.iconLevel = 1;
    mNoticationManager.notify(NOTIFICATION_ID, mNotification);
    

    【讨论】:

    • 这确实是一个比公认的答案更完整,可以说“更正确”的答案,以及我使用和验证的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-15
    • 2018-03-22
    • 2015-07-06
    • 1970-01-01
    • 2021-01-20
    • 2010-12-28
    • 1970-01-01
    相关资源
    最近更新 更多