簡體   English   中英

什么是BigTextStyle通知的最大大小

[英]What's the max size of a BigTextStyle notification

我有一個與Android Wear集成的消息傳遞應用程序。 環聊類似,在Android Wear智能手表中選擇通知時,您可以滑動到顯示與所選消息對應的對話的第二張卡。 我使用BigTextStyle通知實現它,但我需要知道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();

有誰知道適合BigTextStyle通知的確切字符數? 它是否在不同設備之間變化?

簡答限制是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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM