【问题标题】:Getting Detail/Expanded Text from an Android Notification?从 Android 通知中获取详细信息/扩展文本?
【发布时间】:2014-03-11 16:31:31
【问题描述】:
【问题讨论】:
标签:
android
notifications
android-notifications
【解决方案2】:
我在 Android 7 上运行的 Gmail 遇到了类似的问题。
最终,我意识到(在 another thread 的帮助下)他的解决方案与此处的现有答案不同 - 您正在寻找的内容可以通过不同的方式访问:
Bundle extras = statusBarNotification.getNotification().extras;
extras.get(Notification.EXTRA_BIG_TEXT) == null ? null : extras.get(Notification.EXTRA_BIG_TEXT).toString();
这样,您将始终获得 String 或 null,即使您要查找的值最初不是字符串。这样做是因为直接调用 getString(Notification.EXTRA_BIG_TEXT) 会返回 null,至少在某些情况下是这样。
如果还有任何其他值您不确定它们可能存储在哪里,您可以尝试遍历整个附加包,as explained here。
【解决方案3】:
最后,我可以使用这段代码解决这个问题。
ArrayList<StatusBarNotification> groupedNotifications = new ArrayList<>();
for(StatusBarNotification statusBarNotification : mNotificationManager.getActiveNotifications()) {
if(statusBarNotification.getNotification().getGroup().equals(NOTIFICATION_GROUP)) {
groupedNotifications.add(statusBarNotification);
}
}
CharSequence stackNotificationMultiLineText[] = groupedNotifications.get(ZEROTH_INDEX).getNotification().extras.getCharSequenceArray(NotificationCompat.EXTRA_TEXT_LINES);
如果您尝试使用 getCharSequence("android.textLines"),它会返回 null,因为 NotificationCompat.EXTRA_TEXT_LINES 返回的是 CharSequence 数组,而不是单个 CharSequence 对象。
【解决方案4】:
我找到了一种访问扩展通知的更短方法。这适用于 API 24。
如果您在访问getCharSequence("android.textLines") 时收到null,这是因为它实际上返回了一个CharSequence 数组,正如上面Puneet 正确指出的那样。
所以宁愿像这样访问它们:
if (extras.getCharSequenceArray("android.textLines") != null)
text = Arrays.toString(extras.getCharSequenceArray("android.textLines"));