7

我有一个与Android Wear集成的消息应用程序。与环聊类似,在 Android Wear 智能手表中选择通知时,您可以滑动到显示与所选消息对应的对话的第二张卡片。我通过通知实现它BigTextStyle,但我需要知道BigTextStyle支持的最大字符数,以便在对话太大而无法完全适应时正确修剪对话。我在文档中找不到此信息。

经过一番调查,最大字符数约为 5000,至少在 Android Wear 模拟器中是这样。因此,我可以执行以下操作:

// scroll to the bottom of the notification card
NotificationCompat.WearableExtender extender = new NotificationCompat.WearableExtender().setStartScrollBottom(true);

// get conversation messages in a big single text
CharSequence text = getConversationText();

// trim text to its last 5000 chars
int start = Math.max(0, text.length() - 5000);
text = text.subSequence(start, text.length());

// set text into the big text style
NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle().bigText(text);

// build notification
Notification notification = new NotificationCompat.Builder(context).setStyle(style).extend(extender).build();

有谁知道适合BigTextStyle通知的确切字符数?它在不同设备之间会发生变化吗?

4

1 回答 1

10

简短回答 限制为 5120 个字符 (5KB),但您不需要限制您的消息。这是在构建器上为您完成的。

详细解答

在您使用的代码中NotificationCompat.BigTextStyle,内部使用NotificationCompat.Builder.

这就是当你打电话给setBigContentTitle

    /**
     * Overrides ContentTitle in the big form of the template.
     * This defaults to the value passed to setContentTitle().
     */
    public BigTextStyle setBigContentTitle(CharSequence title) {
        mBigContentTitle = Builder.limitCharSequenceLength(title);
        return this;
    }

该功能limitCharSequenceLength执行此操作

    protected static CharSequence limitCharSequenceLength(CharSequence cs) {
        if (cs == null) return cs;
        if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
            cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
        }
        return cs;
    }

如果我们检查常量声明,我们会发现

    /**
     * Maximum length of CharSequences accepted by Builder and friends.
     *
     * <p>
     * Avoids spamming the system with overly large strings such as full e-mails.
     */
    private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
于 2015-07-27T08:18:59.077 回答