【问题标题】:Why doesn't Jelly Bean show the second row in a Notification?为什么果冻豆在通知中不显示第二行?
【发布时间】:2012-08-16 13:19:44
【问题描述】:

我目前正在研究 Android 支持包 v4 Rev 10 的 NotificationCompat 功能。documentation 表示“setContentText()”显示通知中的第二行。这适用于 API 8 到 API 15。但是,如果我尝试在 API 16 中使用此方法,我的通知将错过第二行。我只看到标题,但看不到第二行。添加多行没问题(使用'addline()')。

这是我使用的 NotificationCompat.Builder 的代码:

private NotificationCompat.Builder buildNormal(CharSequence pTitle) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            getSherlockActivity());

    builder.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL);
    // set the shown date
    builder.setWhen(System.currentTimeMillis());
    // the title of the notification
    builder.setContentTitle(pTitle);
    // set the text for pre API 16 devices
    builder.setContentText(pTitle);
    // set the action for clicking the notification
    builder.setContentIntent(buildPendingIntent(Settings.ACTION_SECURITY_SETTINGS));
    // set the notifications icon
    builder.setSmallIcon(android.R.drawable.stat_sys_download_done);
    // set the small ticker text which runs in the tray for a few seconds
    builder.setTicker("This is your ticker text.");
    // set the priority for API 16 devices
    builder.setPriority(Notification.PRIORITY_DEFAULT);
    return builder;
}

如果我想添加多行并显示通知,我使用以下代码:

NotificationCompat.Builder normal = buildNormal("This is an Expanded Layout Notification.");
    NotificationCompat.InboxStyle big = new NotificationCompat.InboxStyle(
            normal);

    // summary is below the action
    big.setSummaryText("this is the summary text");
    // Lines are above the action and below the title
    big.addLine("This is the first line").addLine("The second line")
            .addLine("The third line").addLine("The fourth line");

    NotificationManager manager = getNotificationManager(normal);
    manager.notify(Constants.NOTIFY_ID, big.build());

这是果冻豆的一个想要的功能,setContentText 被忽略还是我错过了什么?代码在所有版本上运行都没有错误,但我想在第二行添加我在 ICS 或更早版本上使用的相同代码。

我还添加了两个屏幕截图。第一个来自我的 ICS 4.0.3 华为 MediaPad,第二个来自 4.1.1 的 Galaxy Nexus。为简单起见,1 的第二行是与通知标题相同的字符串。它在2 上不可见。

提前感谢您的帮助!

【问题讨论】:

    标签: android notifications android-4.2-jelly-bean


    【解决方案1】:

    这是果冻豆的一个想要的功能,setContentText 被忽略还是我错过了什么?

    setContextText() 的值应该在折叠状态下可见(例如,如果展开,两指向上滑动,或者它不是最顶部的 Notification)。鉴于您上面的代码,它将在展开状态下被 NotificationCompat.InboxStyle 替换。

    【讨论】:

    • 谢谢,我不知道这种通知手势。但是没有这个手势就没有办法显示第二行吗?
    • @MarkusRudel:“我不知道这种通知手势”——是的,在有人向我指出之前,我也不知道。 “可是没有这个手势,就没有办法显示第二行吗?” -- 在 InboxStyle 设置中的某处使用相同的文本(例如,setSummaryText())。
    • 看了design guidelines之后,我终于明白了关于通知展开/折叠手势的部分……你甚至可以用捏合手势打开和关闭。
    • 我收到了这条消息,你知道吗? NotificationCOmpat 已正确添加到兼容 lib v4 的项目中并且可以正常工作,但是:NotificationCompat.Style 无法解析为类型
    • @Waza_Be:尝试升级到新版本的 Android 支持包 JAR。
    【解决方案2】:

    如果你想在默认展开状态下显示通知。只需设置构建器的 PRIORITY_MAX。 像: builder.setPriority(Notification.PRIORITY_MAX);

    【讨论】:

      【解决方案3】:

      我现在在@CommonsWare 的帮助下解决了这个问题并创建了一个简单的方法,它将检查当前的 API 级别并决定应该使用什么命令:

      private void createCompatibleSecondLine(CharSequence pTitle,
              NotificationCompat.Builder pBuilder, InboxStyle pInboxStyle) {
          // set the text for pre API 16 devices (or for expanded)
          if (android.os.Build.VERSION.SDK_INT < 16) {
              pBuilder.setContentText(pTitle);
          } else {
              pInboxStyle.setSummaryText(pTitle);
          }
      }
      

      所以它并不疯狂,远非完美,但它为我完成了这项工作。总是欢迎改进:)

      【讨论】:

      • 因为电话很便宜,我会同时拨打setContentText()setSummaryText()
      • 好的,如果两个调用都同样便宜,那么该方法可能没用。方法的调用和附加的 if 子句是否会比仅调用这两个方法花费更多的时间?
      • 调用这两种方法的成本会逐渐增加,但在微秒的数量级上。而且您不是在循环中执行此操作,因此它实际上是微秒,而不是微秒 * 迭代次数。对于这样的微小节省,我会采用更易于维护的方法来调用这两种方法。
      • @MarkusRudel 我已采用您的代码作为我的收件箱样式通知。但是当我在我的三星 android (4.2) 手机中测试时,它显示我处于折叠状态,我只能在缩放手势上看到额外的线条。默认情况下,当我在我的 Sony android (4.2) 手机中测试它时,它会以扩展格式显示给我。知道为什么会这样吗?我想要默认的展开形式。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-10
      • 1970-01-01
      • 2013-02-12
      相关资源
      最近更新 更多