什么是BigTextStyle通知的最大大小

mba*_*ben 7 android android-wear-notification

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

经过一些调查,max chars大约是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();
Run Code Online (Sandbox Code Playgroud)

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

小智 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;
    }
Run Code Online (Sandbox Code Playgroud)

该功能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;
    }
Run Code Online (Sandbox Code Playgroud)

如果我们检查常量声明,我们发现了这一点

    /**
     * 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;
Run Code Online (Sandbox Code Playgroud)