A message processing method and related apparatus

By switching the message parsing task from the main thread to HandlerThread for asynchronous processing in live streaming scenarios and adopting a three-layer caching mechanism, the problem of main thread blocking caused by high-concurrency message push was solved, and stable operation at high frame rate was achieved in the live streaming room.

CN121547633BActive Publication Date: 2026-05-26GUANGZHOU QIANJUN NETWORK TECH CO LTD

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
GUANGZHOU QIANJUN NETWORK TECH CO LTD
Filing Date
2026-01-15
Publication Date
2026-05-26

AI Technical Summary

Technical Problem

In live streaming scenarios, high-concurrency message pushes can cause the main thread to be blocked, resulting in UI rendering stuttering and interface scrolling stuttering, making it impossible to maintain a high frame rate.

Method used

The live stream messages are submitted to the HandlerThread for asynchronous parsing via the main thread. A three-layer caching mechanism is used: ImageSpan caching, message aggregation caching, and SpannableString caching. Differentiated caching strategies are applied to image and text types respectively. The messages are then displayed in the main thread based on the RecycleView's message refresh interval.

Benefits of technology

It significantly reduces the load on the main thread, reduces memory consumption and garbage collection frequency, avoids frame drops and screen stuttering, and ensures that the main thread maintains a high frame rate in high-concurrency scenarios.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121547633B_ABST
    Figure CN121547633B_ABST
Patent Text Reader

Abstract

This invention discloses a message processing method and related apparatus, relating to the field of message processing. When the current message cache is not full, the main thread receives live messages and submits them to a HandlerThread for asynchronous parsing to obtain different message types. Corresponding caching strategies are then applied, retrieving image cache data from the ImageSpan cache and text cache data from the plain text cache, assembling them into a complete live message, and inserting it into a RecycleView queue for message aggregation caching. The complete live message is then inserted into a scrolling control in the RecycleView list for display. Based on a preset scrolling strategy, the complete live message is directly scrolled and displayed using a SpannableString cache. This invention utilizes HandlerThread for asynchronous message processing, a three-layer caching mechanism to optimize performance, and strategy parsing to decouple rendering logic, ensuring the main thread maintains a high frame rate.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of message processing technology, and more specifically, to a message processing method and related apparatus. Background Technology

[0002] In live streaming scenarios, live interaction is a core function for improving user activity, requiring the user side to display a large number of interactive messages in real time (such as bullet comments, entry notifications, gift announcements, etc.). When the live stream is popular, the message push frequency can reach dozens or even hundreds per second.

[0003] Each message requires synchronous parsing of rich text (including multiple ImageSpan images) on the main thread, with each parsing session taking 20ms to 40ms. Since UI (User Interface) rendering follows a 16ms frame interval mechanism (meaning UI updates must be completed approximately every 16ms to maintain smoothness), the single-session time far exceeds this threshold, causing the main thread to be blocked for an extended period, resulting in stuttering. Consequently, UI rendering cannot be completed within the 16ms frame interval, causing dropped frames. Simultaneously, user input events (such as swipe gestures) cannot be responded to in a timely manner due to the main thread being occupied, ultimately leading to stuttering during interface scrolling. Summary of the Invention

[0004] In view of this, the present invention discloses a message processing method and related apparatus to enable the main thread to maintain a high frame rate in high-concurrency scenarios, effectively avoiding problems such as frame drops and interface lag caused by main thread blocking.

[0005] A message processing method, comprising:

[0006] If the current message buffer is not full, the main thread receives the live stream message and submits the live stream message to the message processing thread HandlerThread for asynchronous parsing to obtain different message types, including image type and text type.

[0007] The HandlerThread parses the corresponding caching strategies for the image type and the text type, retrieves the image cache data corresponding to the image type from the pre-created ImageSpan rich text cache, and retrieves the text cache data from the plain text cache according to the text type.

[0008] All the image cache data and text cache data are assembled into a complete live stream message and inserted into the RecycleView queue for message aggregation and caching;

[0009] In the main thread, according to the message refresh interval set by RecycleView, the complete live messages in the RecycleView queue are inserted into the RecycleView list scrolling control for display and assigned to the message model;

[0010] According to the preset scrolling strategy, the complete live message is displayed directly by caching the SpannableString, a styleable string bound to the message model.

[0011] Optionally, the creation process of the ImageSpan cache includes:

[0012] Create a rich text view container called DrawableTextView, and set the user interface parameters in the DrawableTextView;

[0013] The DrawableTextView with the user interface parameters set is converted into a Bitmap image of type image. The Bitmap is a necessary parameter for creating the ImageSpan cache.

[0014] Create a centered image rich text CenterImageSpan using a Bitmap;

[0015] The CenterImageSpan is designated as the ImageSpan cache.

[0016] Optionally, based on the message refresh interval set by the RecycleView, the complete live messages in the RecycleView queue are inserted into the RecycleView list scroll control for display, including:

[0017] The complete live stream message is then retrieved back to the main thread;

[0018] In the main thread, it is determined whether the interval between the current time and the last refresh time is less than the set message refresh interval.

[0019] If so, pause the insertion of the complete live message from the RecycleView queue into the RecycleView list scroll control for display;

[0020] If not, insert the complete live message from the RecycleView queue into the RecycleView list scroll control for display.

[0021] Optionally, the preset scrolling strategy includes:

[0022] Determine if the currently displayed message list is in the latest position at the bottom of the user interface;

[0023] If not, a message unread notification will be displayed at the bottom, wherein the message unread notification includes the number of unread messages;

[0024] If so, trigger the list scrolling strategy when inserting new messages in batches.

[0025] Optionally, the list scrolling strategy includes:

[0026] If the number of currently updated messages is the first quantity threshold, then the automatic scrolling duration of the list will be set to the first preset duration, and the currently updated messages will be pushed out slowly.

[0027] If the number of currently updated messages is the second quantity threshold, then the automatic scrolling duration of the list is set to the second preset duration, and the currently updated messages are pushed out slightly faster. The second quantity threshold is greater than the first quantity threshold, and the second preset duration is less than the first preset duration.

[0028] If the number of currently updated messages is greater than the third quantity threshold, the automatic scrolling duration of the list is set to the third preset duration, and a fast push effect is applied to the currently updated messages. The third quantity threshold is greater than the second quantity threshold, and the third preset duration is less than the second preset duration.

[0029] If the number of currently updated messages is greater than the fourth threshold, then the automatic scrolling duration of the list is set to the system default scrolling duration, where the fourth threshold is greater than the third threshold.

[0030] Optionally, it also includes:

[0031] When a user leaves the live stream, clear all cached live stream resources.

[0032] Optionally, it also includes:

[0033] If the current message buffer is full, determine whether the user has set a message discarding policy;

[0034] If so, the received live stream message will be discarded directly;

[0035] If not, the received live stream messages will be cached in the corresponding container.

[0036] A message processing apparatus, comprising:

[0037] The asynchronous parsing unit is used to receive live messages through the main thread when the current message buffer is not full, and submit the live messages to the message processing thread HandlerThread for asynchronous parsing to obtain different message types, including image type and text type.

[0038] The cache data acquisition unit is used to parse the corresponding caching strategies for the image type and the text type through the HandlerThread, obtain the image cache data corresponding to the image type from the pre-created ImageSpan rich text cache, and obtain the text cache data from the plain text cache according to the text type.

[0039] The message assembly unit is used to assemble all the image cache data and text cache data into a complete live message and insert it into the RecycleView queue for message aggregation caching.

[0040] The message insertion unit is used in the main thread to insert the complete live message in the RecycleView queue into the RecycleView list scrolling control for display according to the message refresh time interval set by the RecycleView, and assign it to the message model.

[0041] The message display unit is used to directly scroll and display the complete live message through the SpannableString cache bound to the message model according to a preset scrolling strategy.

[0042] A computer storage medium storing at least one instruction, which, when executed by a processor, implements the message processing method described above.

[0043] An electronic device, comprising: a memory and a processor;

[0044] The memory is used to store at least one instruction;

[0045] The processor is used to execute the at least one instruction to implement the message processing method described above.

[0046] As can be seen from the above technical solution, this invention discloses a message processing method and related apparatus. When the current message buffer is not full, the main thread receives live messages and submits them to a HandlerThread for asynchronous parsing to obtain different message types. The HandlerThread parses image and text types according to corresponding caching strategies, retrieves image cache data corresponding to image types from a pre-created ImageSpan cache, and retrieves text cache data from a plain text cache according to text types. All image and text cache data are assembled into a complete live message and inserted into a RecycleView queue for message aggregation caching. In the main thread, according to the message refresh interval set by the RecycleView, the complete live message in the RecycleView queue is inserted into the RecycleView list scrolling control for display and assigned to the message model. According to a preset scrolling strategy, the complete live message is directly scrolled and displayed through the SpannableString cache bound to the message model. This invention significantly reduces the load pressure on the main thread by switching the live message parsing task from the main thread to a HandlerThread for asynchronous processing. Simultaneously, a three-layer caching mechanism is employed to optimize performance: ImageSpan caching effectively reduces memory overhead and garbage collection frequency through reuse; message aggregation caching pre-merges batch messages, reducing the number of RecyclerView layout calculations; and SpannableString caching addresses message scrolling scenarios, avoiding redundant calculations during message scrolling. Furthermore, this invention designs differentiated caching strategies for image and text types, and decouples rendering logic through strategy parsing. This design allows the system to dynamically adapt to different strategy combinations without modifying the core code when receiving batch messages, ensuring that the main thread maintains a high frame rate even in high-concurrency scenarios, thus effectively avoiding issues such as frame drops and interface lag caused by main thread blocking. Attached Figure Description

[0047] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on the published drawings without creative effort.

[0048] Figure 1 This is a flowchart of a message processing method disclosed in an embodiment of the present invention;

[0049] Figure 2 This is a schematic diagram of the structure of a message processing device disclosed in an embodiment of the present invention;

[0050] Figure 3 This is a schematic diagram of the structure of an electronic device disclosed in an embodiment of the present invention. Detailed Implementation

[0051] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0052] This invention discloses a message processing method and related apparatus. By switching the parsing task of live messages from the main thread to a HandlerThread for asynchronous processing, the load pressure on the main thread is significantly reduced. Simultaneously, a three-layer caching mechanism is employed for performance optimization: ImageSpan caching reuse effectively reduces memory overhead and garbage collection frequency; message aggregation caching pre-merges batch messages, reducing the number of RecyclerView layout calculations; and SpannableString caching is designed for message scrolling scenarios, avoiding redundant calculations during message scrolling. Furthermore, this invention designs differentiated caching strategies for image and text types, and decouples rendering logic through strategy parsing. This design allows the system to dynamically adapt to different strategy combinations when receiving batch messages without modifying the core code, ensuring that the main thread maintains a high frame rate even in high-concurrency scenarios, thereby effectively avoiding problems such as dropped frames and stuttering during interface scrolling caused by main thread blocking.

[0053] Explanation of related terms

[0054] ImageSpan (Image Rich Text): A rich text style object in the Android system used to embed images within text.

[0055] SpannableStringBuilder: Android provides a spannable string builder that supports dynamically adding styles.

[0056] HandlerThread (Message Processing Thread): A child thread in Android that comes with Looper, which can be used to execute asynchronous tasks and supports message queue communication.

[0057] See Figure 1 The present invention discloses a message processing method flowchart, which includes the following steps:

[0058] Step S101: If the current message buffer is not full, receive the live message through the main thread and submit the live message to the HandlerThread for asynchronous parsing to obtain different message types.

[0059] If the current message buffer is not full, the main thread receives the live message and enters the message parsing stage. The live message is submitted to the HandlerThread, which then performs asynchronous parsing to obtain different message types.

[0060] Message types include image types and text types. Image types include tags such as user level labels, honor badges, user outfits, avatars, nicknames, and user titles.

[0061] In addition to image-based tags, live stream messages also include text-based messages such as interactive messages and frequently used slogans.

[0062] Step S102: The HandlerThread performs corresponding caching strategy parsing on the image type and the text type, retrieves the image cache data corresponding to the image type from the pre-created ImageSpan cache, and retrieves the text cache data from the plain text cache according to the text type.

[0063] HandlerThread parses the corresponding caching strategies for image and text types respectively. Specifically, it first checks if there is an ImageSpan cache corresponding to the image type tag. If it exists, it directly retrieves the corresponding image cache data from the ImageSpan cache. If it does not exist, it creates a new ImageSpan cache for future use.

[0064] Existing solutions do not implement a caching mechanism for ImageSpans corresponding to tags of the same image type. This results in the need to recreate Bitmaps and Drawables to generate ImageSpan caches every time a message is parsed. This process not only significantly increases memory usage but also easily triggers frequent garbage collection (GC), leading to wasted memory resources. This invention effectively reduces memory overhead and decreases GC frequency by reusing ImageSpan caches.

[0065] It should be noted that in the scheme designed in this application, the ImageSpan cache, as the first-level cache layer, adopts a global caching strategy based on tag content, directly caching the generated ImageSpan instances (i.e., the encapsulated image cache data). By reusing the ImageSpan instances in the cache, the repeated creation of image objects with the same content can be avoided, thereby effectively reducing memory overhead and reducing GC frequency.

[0066] Besides image-type tags, text messages such as interactive messages and frequently used slogans also have corresponding caching strategies. Unlike image caching, the triggering conditions for plain text caching are different—because text content itself has low repetition, the text caching mechanism is only triggered when it appears repeatedly in specific scenarios (such as event slogans or frequently used interactive messages). Specifically, text caching uses the message content itself as the key, directly matching the text content when a message is received: if a match exists in the cache, it is directly invoked; otherwise, a preset text caching rule is used to determine whether the current text needs to be cached. This rule can be customized by the user, for example, setting "caching is triggered when the same type of message appears more than 3 times cumulatively."

[0067] Step S103: Assemble all the image cache data and text cache data into a complete live message and insert it into the RecycleView queue for message aggregation and caching.

[0068] RecycleView, or Circulating List View, is a core component in Android development used for efficiently displaying large lists of dynamic data. Its core function is to optimize memory usage and performance through view recycling and reuse mechanisms, making it particularly suitable for scenarios requiring frequent scrolling, dynamic updates, or the display of large amounts of data.

[0069] In practical applications, you can use SpannableStringBuilder to concatenate all the image cache data and text cache data to obtain the complete live stream message.

[0070] After assembling all the image cache data and text cache data into a complete live stream message, this application inserts the message into the RecycleView queue after the message parsing is completed for message aggregation and caching. Then, according to the message refresh interval set in the RecycleView, the messages in the queue are inserted into the RecycleView list scrolling control for display.

[0071] It should be noted that in the scheme designed in this application, message aggregation caching serves as the second-level caching layer. The core mechanism of message aggregation caching is to temporarily store the assembled complete live messages in the RecyclerView queue, and then batch insert them into list controls for display according to a preset message refresh interval. Therefore, refresh operations can be merged, reducing the number of RecyclerView layout calculations. During batch insertion, RecyclerView can more efficiently reuse views in the recycling pool, thereby reducing the frequency of memory allocation.

[0072] Step S104: In the main thread, according to the message refresh time interval set by RecycleView, insert the complete live messages in the RecycleView queue into the RecycleView list scrolling control for display, and assign them to the message model.

[0073] The value of the message refresh time interval depends on the actual needs, and this invention does not limit it.

[0074] Step S105: According to the preset scrolling strategy, the complete live message is directly scrolled and displayed through the SpannableString cache bound to the message model.

[0075] The Chinese explanation of SpannableString is "styleable string". SpannableString is the core class used in Android development to handle rich text styles. Its core principle is to achieve differentiated style control of parts of the text in the string (such as color, font, underline, click event, etc.) through the combination of "text content + style markup".

[0076] In practical applications, a preset scrolling strategy can be set for RecycleView as needed, and the list can be refreshed according to the preset scrolling strategy.

[0077] It should be noted that in the scheme designed in this application, the SpannableString cache is used as a third-level caching layer. Specifically, based on a preset scrolling strategy, the system directly scrolls and displays the complete live stream message through the SpannableString cache bound to the message model. Afterward, regardless of how the user scrolls the message list, the already parsed data does not need to be parsed again; instead, it is directly displayed using the SpannableString cache bound to the message model.

[0078] For example, if the number of messages to be scrolled is 10, the scrolling time can be set to 50 milliseconds. Generally, the more messages there are, the shorter the scrolling time should be to ensure that the entire scrolling process can be completed quickly; while the fewer messages there are, a slightly longer scrolling time can be set so that users can clearly see the message scrolling effect in an orderly manner.

[0079] In summary, this invention discloses a message processing method. When the current message buffer is not full, the main thread receives live messages and submits them to a HandlerThread for asynchronous parsing to obtain different message types. The HandlerThread parses image and text types according to corresponding caching strategies, retrieving image cache data corresponding to image types from a pre-created ImageSpan cache and text cache data from a plain text cache based on text types. All image and text cache data are assembled into a complete live message and inserted into a RecycleView queue for message aggregation caching. In the main thread, according to the message refresh interval set by the RecycleView, the complete live message in the RecycleView queue is inserted into the RecycleView list scrolling control for display and assigned to the message model. According to a preset scrolling strategy, the complete live message is directly scrolled and displayed through the SpannableString cache bound to the message model. This invention significantly reduces the load on the main thread by switching the live message parsing task from the main thread to a HandlerThread for asynchronous processing. Simultaneously, a three-layer caching mechanism is employed to optimize performance: ImageSpan caching effectively reduces memory overhead and garbage collection frequency through reuse; message aggregation caching pre-merges batch messages, reducing the number of RecyclerView layout calculations; and SpannableString caching addresses message scrolling scenarios, avoiding redundant calculations during message scrolling. Furthermore, this invention designs differentiated caching strategies for image and text types, and decouples rendering logic through strategy parsing. This design allows the system to dynamically adapt to different strategy combinations without modifying the core code when receiving batch messages, ensuring that the main thread maintains a high frame rate even in high-concurrency scenarios, thus effectively avoiding issues such as frame drops and interface lag caused by main thread blocking.

[0080] Furthermore, this invention achieves complete decoupling between image tagging and rendering logic through a strategy parsing mechanism: when adding a new image tag, only the corresponding generation strategy needs to be extended (such as a badge dynamic effect strategy, a dress-up layered rendering strategy, etc.), without modifying the core rendering code. This design significantly reduces system maintenance costs while possessing high scalability, enabling rapid support for future additions of various image tag requirements.

[0081] In one embodiment, the process of creating an ImageSpan cache may include:

[0082] (1) Create a DrawableTextView and set the user interface parameters in the DrawableTextView.

[0083] DrawableTextView (rich text view container) is a custom Android view component whose core function is to support the dynamic embedding of various drawable objects in text and to achieve flexible rendering and interaction of rich text.

[0084] User interface parameters include, but are not limited to, style, font size, icons, etc.

[0085] (2) Convert the DrawableTextView with the user interface parameters set into a Bitmap of image type.

[0086] Bitmap is a necessary parameter for creating the ImageSpan cache.

[0087] (3) Create CenterImageSpan using Bitmap.

[0088] CenterImageSpan (a rich text image that can be centered) is an ImageSpan with a centered style.

[0089] (4) The CenterImageSpan is determined as the ImageSpan cache.

[0090] For example, taking image-type tags as user titles, the process of asynchronously parsing HandlerThread and retrieving corresponding cached data through ImageSpan is illustrated below:

[0091] 1) The live stream message contains user titles that need to be displayed. The display effect of the user title is an image type, so it will be distributed to the generation strategy corresponding to the user title. The generation strategy includes cache checks and the generation rules of the title ImageSpan.

[0092] 2) During the matching process for user title strategies, the system will first check the title cache list to confirm whether there is an ImageSpan (i.e., the image display data corresponding to the title) for the current user title. The matching basis is the unique identifier of each title (which can be understood as the title ID), and the corresponding image resource can be quickly located in the cache through this ID.

[0093] 3) If the user's title is not cached in the title cache list, the process of creating a new cache will begin:

[0094] ① Create a DrawableTextView (rich text view container). The DrawableTextView is the carrier for displaying the new title and is used to create an ImageSpan that can be cached.

[0095] ② Set the UI parameters such as color, style, font size, and icon for the title according to the title design requirements.

[0096] ③ After successfully creating a new title object, the entire DrawableTextView is converted into a Bitmap image. Bitmap is a necessary parameter for creating an ImageSpan cache object. CenterImageSpan, which is a centered ImageSpan, is created through Bitmap.

[0097] ④ After successfully creating a CenterImageSpan, the system will add it to the cache: using the unique identifier (ID) of the title as the key and the newly generated CenterImageSpan as the value, it will be cached in the ImageSpanCacheList (image rich text cache list) queue as a key-value pair. If the same title is encountered later, the corresponding CenterImageSpan can be quickly retrieved from the cache directly by the title ID, avoiding duplicate creation.

[0098] 4) If cached data for a user's title exists in the title cache list, retrieve the corresponding cached data for the next step of message assembly.

[0099] Image tags similar to titles (such as user level tags, honor badges, user outfits, etc.) all use the same caching mechanism as the titles mentioned above: key-value pairs are established using unique identifiers (IDs) and cached in ImageSpanCacheList. The difference lies in the fact that the UI generation rules for various image types of tags differ (for example, badges may require dynamic effects, and outfits may involve multiple layers). Therefore, an independent generation strategy needs to be designed for each type of tag to ensure that the final rendering effect conforms to the design specifications.

[0100] In one embodiment, step S104 may specifically include:

[0101] (1) Recall the complete live stream message back to the main thread;

[0102] The process of asynchronously parsing the live stream message and ultimately assembling it into a complete live stream message is executed in the HandlerThread thread. After obtaining the complete live stream message, it needs to be called back to the main thread for display.

[0103] (2) In the main thread, determine whether the interval between the current time and the last refresh time is less than the set message refresh interval;

[0104] The main thread maintains a message buffer to be displayed, and displays live messages according to the message refresh interval set by the user.

[0105] The message refresh interval value is determined according to actual needs, such as 600ms.

[0106] (3) If so, pause the insertion of the complete live message in the RecycleView queue into the RecycleView list scroll control for display;

[0107] (4) If not, insert the complete live message in the RecycleView queue into the RecycleView list scroll control for display.

[0108] Specifically, it checks whether the interval between the current time and the last refresh time is less than the set message refresh interval. If so, it pauses the insertion of complete live messages from the RecycleView queue into the RecycleView list scroll control for display, meaning the message list buffer to be displayed is not operated. If not, it inserts complete live messages from the RecycleView queue into the RecycleView list scroll control for display, that is, it merges the buffer with the current message, calls notifyItemRangeInserted (notify multiple items to insert) for batch refresh, and clears the buffer.

[0109] This application supports configuring a message buffer capacity limit (e.g., setting it to 20 messages). When the buffer reaches the limit, the system will choose to discard newly received messages or historical messages to free up space based on a policy.

[0110] In one embodiment, the preset scrolling strategy involved in step S105 includes:

[0111] Determine if the currently displayed message list is in the latest position at the bottom of the user interface;

[0112] If not, a message unread notification will be displayed at the bottom, wherein the message unread notification includes the number of unread messages;

[0113] If so, trigger the list scrolling strategy when inserting new messages in batches.

[0114] Specifically, after the user interface refreshes, it checks whether the currently displayed message list is in the latest position at the bottom of the user interface. If the user manually scrolls, the message display may not be in the latest position at the bottom of the user interface. If it is not in the latest position at the bottom, an unread message prompt is displayed at the bottom, such as "X messages remain unread"; if it is in the latest position at the bottom (including clicking the unread message prompt), the user scrolls to the bottom, and in the case of batch insertion of new messages, the list scrolling strategy is triggered.

[0115] The list scrolling strategies include:

[0116] If the number of currently updated messages is the first quantity threshold, then the automatic scrolling duration of the list will be set to the first preset duration, and the currently updated messages will be pushed out slowly.

[0117] If the number of currently updated messages is the second quantity threshold, then the automatic scrolling duration of the list is set to the second preset duration, and the currently updated messages are pushed out slightly faster. The second quantity threshold is greater than the first quantity threshold, and the second preset duration is less than the first preset duration.

[0118] If the number of currently updated messages is greater than the third quantity threshold, the automatic scrolling duration of the list is set to the third preset duration, and a fast push effect is applied to the currently updated messages. The third quantity threshold is greater than the second quantity threshold, and the third preset duration is less than the second preset duration.

[0119] If the number of currently updated messages is greater than the fourth threshold, then the automatic scrolling duration of the list is set to the system default scrolling duration, where the fourth threshold is greater than the third threshold.

[0120] In this embodiment, the value of the first quantity threshold is determined according to actual needs, for example, it is 1, and the value of the first preset duration is determined according to actual needs, for example, 300ms.

[0121] The value of the second quantity threshold is determined according to actual needs, for example, 3. The value of the second preset duration is determined according to actual needs, for example, 200ms.

[0122] The value of the third quantity threshold is determined according to actual needs, for example, 5. The value of the third preset duration is determined according to actual needs, for example, 100ms.

[0123] The value of the fourth quantity threshold is determined according to actual needs, for example, 15. The value of the system default scrolling duration is determined according to actual needs, for example, 25ms.

[0124] For example, when the current messages are at the bottom, inserting new messages in batches will trigger the list scrolling strategy.

[0125] 1) If the current number of updated messages is 1, set the scrolling time to 300ms for a slow rollout effect.

[0126] 2) If the current number of updated messages is 3, set the scrolling time to 200ms for a slightly faster rollout effect.

[0127] 3) If the number of currently updated messages is greater than 5, set the scrolling duration to 100ms for a fast scrolling effect.

[0128] 4) For more messages, the scrolling duration is set to 25ms. 25ms is the system default scrolling duration to cope with high-frequency scenarios.

[0129] The specific scrolling strategy needs to be customized based on the actual message area height and the required scrolling speed, which can result in different message delivery effects.

[0130] This invention dynamically adjusts the animation speed of message delivery based on the number of messages inserted in batches, which can greatly improve the user experience.

[0131] As can be seen from the above discussion, this invention uses a large number of caching mechanisms. Therefore, after the user leaves the live broadcast room, the cached data of this device needs to be destroyed and recycled to avoid memory leaks and affect the stability of the application.

[0132] Therefore, message processing methods may also include:

[0133] When a user leaves the live stream, clear all cached live stream resources.

[0134] For example, 1. Exit AsyncHandler (asynchronous parsing thread) to exit the asynchronous parsing thread and avoid the asynchronous parsing thread from continuously waiting and occupying memory, which increases consumption.

[0135] 2. Clear various caching strategies. During initialization, various tag generation strategies are passed in. These strategies need to be cleared to reduce memory usage.

[0136] 3. Clear the cache of user level tags, honor badges, user outfits, interactive messages, and high-frequency slogans. The larger the traffic, the more users, and the more frequent the messages in the live broadcast room, the larger the cached data will be. It is necessary to clear the cache after exiting the live broadcast room to free up space and ensure that it does not affect the normal operation of the application.

[0137] Figure 1 The illustrated embodiment describes the message processing procedure performed when the current message buffer is not full. When the current message buffer is full, the following procedure is performed:

[0138] If the current message buffer is full, determine whether the user has set a message discarding policy;

[0139] If so, the received live stream message will be discarded directly;

[0140] If not, the received live stream messages will be cached in the corresponding container.

[0141] In practical applications, when a client receives live stream messages pushed by the server, it first checks if the current message buffer is full. If the buffer is full, it then checks the message discarding policy. The message discarding policy is pre-configured by the user. If a policy is set, received live stream messages are discarded directly without any processing. If no policy is set, received live stream messages are placed in the corresponding container for caching. When the container is full, messages are removed using a first-in, first-out (FIFO) approach. For example, if a user needs to temporarily store 20 discarded messages, these messages are stored sequentially in an array; if the array exceeds 20, the oldest message is removed.

[0142] Corresponding to the above method embodiments, the present invention also discloses a message processing device.

[0143] See Figure 2 A schematic diagram of a message processing device disclosed in an embodiment of the present invention is shown. The device includes:

[0144] The asynchronous parsing unit 201 is used to receive live messages through the main thread when the current message buffer is not full, and submit the live messages to the message processing thread HandlerThread for asynchronous parsing to obtain different message types.

[0145] If the current message buffer is not full, the main thread receives the live message and enters the message parsing stage. The live message is submitted to the HandlerThread, which then performs asynchronous parsing to obtain different message types.

[0146] Message types include image types and text types. Image types include tags such as user level labels, honor badges, user outfits, avatars, nicknames, and user titles.

[0147] In addition to image-based tags, live stream messages also include text-based messages such as interactive messages and frequently used slogans.

[0148] The cache data acquisition unit 202 is used to parse the corresponding caching strategies for the image type and the text type through the HandlerThread, acquire the image cache data corresponding to the image type from the pre-created ImageSpan rich text cache, and acquire the text cache data from the plain text cache according to the text type.

[0149] HandlerThread parses the corresponding caching strategies for image and text types respectively. Specifically, it first checks if there is an ImageSpan cache corresponding to the image type tag. If it exists, it directly retrieves the corresponding image cache data from the ImageSpan cache. If it does not exist, it creates a new ImageSpan cache for future use.

[0150] Existing solutions do not implement a caching mechanism for ImageSpans corresponding to tags of the same image type. This results in the need to recreate Bitmaps and Drawables to generate ImageSpan caches every time a message is parsed. This process not only significantly increases memory usage but also easily triggers frequent garbage collection (GC), leading to wasted memory resources. This invention effectively reduces memory overhead and decreases GC frequency by reusing ImageSpan caches.

[0151] It should be noted that in the scheme designed in this application, the ImageSpan cache, as the first-level cache layer, adopts a global caching strategy based on tag content, directly caching the generated ImageSpan instances (i.e., the encapsulated image cache data). By reusing the ImageSpan instances in the cache, the repeated creation of image objects with the same content can be avoided, thereby effectively reducing memory overhead and reducing GC frequency.

[0152] Besides image-type tags, text messages such as interactive messages and frequently used slogans also have corresponding caching strategies. Unlike image caching, the triggering conditions for plain text caching are different—because text content itself has low repetition, the text caching mechanism is only triggered when it appears repeatedly in specific scenarios (such as event slogans or frequently used interactive messages). Specifically, text caching uses the message content itself as the key, directly matching the text content when a message is received: if a match exists in the cache, it is directly invoked; otherwise, a preset text caching rule is used to determine whether the current text needs to be cached. This rule can be customized by the user, for example, setting "caching is triggered when the same type of message appears more than 3 times cumulatively."

[0153] The message assembly unit 203 is used to assemble all the image cache data and the text cache data into a complete live message and insert it into the RecycleView queue for message aggregation caching.

[0154] In practical applications, you can use SpannableStringBuilder to concatenate all the image cache data and text cache data to obtain the complete live stream message.

[0155] After assembling all the image cache data and text cache data into a complete live stream message, this application inserts the message into the RecycleView queue after the message parsing is completed for message aggregation and caching. Then, according to the message refresh interval set in the RecycleView, the messages in the queue are inserted into the RecycleView list scrolling control for display.

[0156] It should be noted that in the scheme designed in this application, message aggregation caching serves as the second-level caching layer. The core mechanism of message aggregation caching is to temporarily store the assembled complete live messages in the RecyclerView queue, and then batch insert them into list controls for display according to a preset message refresh interval. Therefore, refresh operations can be merged, reducing the number of RecyclerView layout calculations. During batch insertion, RecyclerView can more efficiently reuse views in the recycling pool, thereby reducing the frequency of memory allocation.

[0157] The message insertion unit 204 is used in the main thread to insert complete live messages in the RecycleView queue into the RecycleView list scrolling control for display according to the message refresh time interval set by the RecycleView, and assign them to the message model.

[0158] The message display unit 205 is used to directly scroll and display the complete live message through the SpannableString cache bound to the message model according to a preset scrolling strategy.

[0159] The Chinese explanation of SpannableString is "styleable string". SpannableString is the core class used in Android development to handle rich text styles. Its core principle is to achieve differentiated style control of parts of the text in the string (such as color, font, underline, click event, etc.) through the combination of "text content + style markup".

[0160] In practical applications, a preset scrolling strategy can be set for RecycleView as needed, and the list can be refreshed according to the preset scrolling strategy.

[0161] It should be noted that in the scheme designed in this application, the SpannableString cache is used as a third-level caching layer. Specifically, based on a preset scrolling strategy, the system directly scrolls and displays the complete live stream message through the SpannableString cache bound to the message model. Afterward, regardless of how the user scrolls the message list, the already parsed data does not need to be parsed again; instead, it is directly displayed using the SpannableString cache bound to the message model.

[0162] For example, if the number of messages to be scrolled is 10, the scrolling time can be set to 50 milliseconds. Generally, the more messages there are, the shorter the scrolling time should be to ensure that the entire scrolling process can be completed quickly; while the fewer messages there are, a slightly longer scrolling time can be set so that users can clearly see the message scrolling effect in an orderly manner.

[0163] In summary, this invention discloses a message processing device. When the current message buffer is not full, the main thread receives live messages and submits them to a HandlerThread for asynchronous parsing to obtain different message types. The HandlerThread parses image and text types according to corresponding caching strategies, retrieving image cache data corresponding to image types from a pre-created ImageSpan cache and text cache data from a plain text cache based on text types. All image and text cache data are assembled into a complete live message and inserted into a RecycleView queue for message aggregation caching. In the main thread, according to the message refresh interval set by the RecycleView, the complete live message in the RecycleView queue is inserted into the RecycleView list scrolling control for display and assigned to the message model. According to a preset scrolling strategy, the complete live message is directly scrolled and displayed through the SpannableString cache bound to the message model. This invention significantly reduces the load on the main thread by switching the live message parsing task from the main thread to a HandlerThread for asynchronous processing. Simultaneously, a three-layer caching mechanism is employed to optimize performance: ImageSpan caching effectively reduces memory overhead and garbage collection frequency through reuse; message aggregation caching pre-merges batch messages, reducing the number of RecyclerView layout calculations; and SpannableString caching addresses message scrolling scenarios, avoiding redundant calculations during message scrolling. Furthermore, this invention designs differentiated caching strategies for image and text types, and decouples rendering logic through strategy parsing. This design allows the system to dynamically adapt to different strategy combinations without modifying the core code when receiving batch messages, ensuring that the main thread maintains a high frame rate even in high-concurrency scenarios, thus effectively avoiding issues such as frame drops and interface lag caused by main thread blocking.

[0164] Furthermore, this invention achieves complete decoupling between image tagging and rendering logic through a strategy parsing mechanism: when adding a new image tag, only the corresponding generation strategy needs to be extended (such as a badge dynamic effect strategy, a dress-up layered rendering strategy, etc.), without modifying the core rendering code. This design significantly reduces system maintenance costs while possessing high scalability, enabling rapid support for future additions of various image tag requirements.

[0165] In one embodiment, the message processing apparatus further includes:

[0166] The image-rich text creation unit is used for:

[0167] Create a rich text view container called DrawableTextView, and set the user interface parameters in the DrawableTextView;

[0168] The DrawableTextView with the user interface parameters set is converted into a Bitmap image of type image. The Bitmap is a necessary parameter for creating the ImageSpan cache.

[0169] Create a centered image rich text CenterImageSpan using a Bitmap;

[0170] The CenterImageSpan is designated as the ImageSpan cache.

[0171] In one embodiment, the message insertion unit 204 can be specifically used for:

[0172] The complete live stream message is then retrieved back to the main thread;

[0173] In the main thread, it is determined whether the interval between the current time and the last refresh time is less than the set message refresh interval.

[0174] If so, pause the insertion of the complete live message from the RecycleView queue into the RecycleView list scroll control for display;

[0175] If not, insert the complete live message from the RecycleView queue into the RecycleView list scroll control for display.

[0176] The preset scrolling strategies include:

[0177] Determine if the currently displayed message list is in the latest position at the bottom of the user interface;

[0178] If not, a message unread notification will be displayed at the bottom, wherein the message unread notification includes the number of unread messages;

[0179] If so, trigger the list scrolling strategy when inserting new messages in batches.

[0180] The list scrolling strategy in this embodiment includes:

[0181] If the number of currently updated messages is the first quantity threshold, then the automatic scrolling duration of the list will be set to the first preset duration, and the currently updated messages will be pushed out slowly.

[0182] If the number of currently updated messages is the second quantity threshold, then the automatic scrolling duration of the list is set to the second preset duration, and the currently updated messages are pushed out slightly faster. The second quantity threshold is greater than the first quantity threshold, and the second preset duration is less than the first preset duration.

[0183] If the number of currently updated messages is greater than the third quantity threshold, the automatic scrolling duration of the list is set to the third preset duration, and a fast push effect is applied to the currently updated messages. The third quantity threshold is greater than the second quantity threshold, and the third preset duration is less than the second preset duration.

[0184] If the number of currently updated messages is greater than the fourth threshold, then the automatic scrolling duration of the list is set to the system default scrolling duration, where the fourth threshold is greater than the third threshold.

[0185] In one embodiment, the message processing apparatus may further include:

[0186] The cleanup unit is used to clear all live stream cache resources when a user leaves the live stream room.

[0187] In one embodiment, the message processing apparatus may further include:

[0188] The judgment unit is used to determine whether the user has set a message discarding policy when the current message buffer is full.

[0189] The discard unit is used to directly discard the received live message if the judgment unit determines that it is true.

[0190] The caching unit is used to cache the received live messages in the corresponding container if the judgment unit determines otherwise.

[0191] It should be noted that for the specific working principles of each component in the device embodiment, please refer to the corresponding section of the method embodiment, which will not be repeated here.

[0192] Corresponding to the above embodiments, the present invention also discloses a computer storage medium that stores at least one instruction, which, when executed by a processor, implements the steps shown in the message processing method embodiments.

[0193] Corresponding to the above embodiments, such as Figure 3 As shown, the present invention also provides a schematic diagram of the structure of an electronic device, which may include: a processor 1 and a memory 2;

[0194] The processor 1 and memory 2 communicate with each other via the communication bus 3.

[0195] Processor 1, for executing at least one instruction;

[0196] Memory 2 is used to store at least one instruction;

[0197] Processor 1 may be a central processing unit (CPU), an application-specific integrated circuit (ASIC), or one or more integrated circuits configured to implement embodiments of the present invention.

[0198] Memory 2 may include high-speed RAM memory, and may also include non-volatile memory, such as at least one disk storage device.

[0199] The processor executes at least one instruction to implement the steps shown in the message processing method embodiment.

[0200] Finally, it should be noted that in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0201] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on the differences from other embodiments. The same or similar parts between the various embodiments can be referred to each other.

[0202] The above description of the disclosed embodiments enables those skilled in the art to make or use the invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the invention. Therefore, the invention is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.

Claims

1. A message processing method, characterized in that, include: If the current message buffer is not full, the main thread receives the live stream message and submits the live stream message to the message processing thread HandlerThread for asynchronous parsing to obtain different message types, including image type and text type. The HandlerThread parses the corresponding caching strategies for the image type and the text type, retrieves the image cache data corresponding to the image type from the pre-created ImageSpan rich text cache, and retrieves the text cache data from the plain text cache according to the text type. All the image cache data and text cache data are assembled into a complete live stream message and inserted into the RecycleView queue for message aggregation and caching; In the main thread, according to the message refresh interval set by RecycleView, the complete live messages in the RecycleView queue are inserted into the RecycleView list scrolling control for display and assigned to the message model; According to the preset scrolling strategy, the complete live message is displayed directly by caching the SpannableString, a styleable string bound to the message model.

2. The message processing method according to claim 1, characterized in that, The creation process of the ImageSpan cache includes: Create a rich text view container called DrawableTextView, and set the user interface parameters in the DrawableTextView; The DrawableTextView with the user interface parameters set is converted into a Bitmap image of type image. The Bitmap is a necessary parameter for creating the ImageSpan cache. Create a centered image rich text CenterImageSpan using a Bitmap; The CenterImageSpan is designated as the ImageSpan cache.

3. The message processing method according to claim 1 or 2, characterized in that, Based on the message refresh interval set in the RecycleView, the complete live messages in the RecycleView queue are inserted into the RecycleView list scroll control for display, including: The complete live stream message is then retrieved back to the main thread; In the main thread, it is determined whether the interval between the current time and the last refresh time is less than the set message refresh interval. If so, pause the insertion of the complete live message from the RecycleView queue into the RecycleView list scroll control for display; If not, insert the complete live message from the RecycleView queue into the RecycleView list scroll control for display.

4. The message processing method according to claim 1 or 2, characterized in that, The preset scrolling strategy includes: Determine if the currently displayed message list is in the latest position at the bottom of the user interface; If not, a message unread notification will be displayed at the bottom, wherein the message unread notification includes the number of unread messages; If so, trigger the list scrolling strategy when inserting new messages in batches.

5. The message processing method according to claim 4, characterized in that, The list scrolling strategy includes: If the number of currently updated messages is the first quantity threshold, then the automatic scrolling duration of the list will be set to the first preset duration, and the currently updated messages will be pushed out slowly. If the number of currently updated messages is the second quantity threshold, then the automatic scrolling duration of the list is set to the second preset duration, and the currently updated messages are pushed out slightly faster. The second quantity threshold is greater than the first quantity threshold, and the second preset duration is less than the first preset duration. If the number of currently updated messages is greater than the third quantity threshold, the automatic scrolling duration of the list is set to the third preset duration, and a fast push effect is applied to the currently updated messages. The third quantity threshold is greater than the second quantity threshold, and the third preset duration is less than the second preset duration. If the number of currently updated messages is greater than the fourth threshold, then the automatic scrolling duration of the list is set to the system default scrolling duration, where the fourth threshold is greater than the third threshold.

6. The message processing method according to claim 1, characterized in that, Also includes: When a user leaves the live stream, clear all cached live stream resources.

7. The message processing method according to claim 1, characterized in that, Also includes: If the current message buffer is full, determine whether the user has set a message discarding policy; If so, the received live stream message will be discarded directly; If not, the received live stream messages will be cached in the corresponding container.

8. A message processing device, characterized in that, include: The asynchronous parsing unit is used to receive live messages through the main thread when the current message buffer is not full, and submit the live messages to the message processing thread HandlerThread for asynchronous parsing to obtain different message types, including image type and text type. The cache data acquisition unit is used to parse the corresponding caching strategies for the image type and the text type through the HandlerThread, acquire the image cache data corresponding to the image type from the pre-created ImageSpan rich text cache, and acquire the text cache data from the plain text cache according to the text type. The message assembly unit is used to assemble all the image cache data and text cache data into a complete live message and insert it into the RecycleView queue for message aggregation and caching. The message insertion unit is used in the main thread to insert the complete live messages in the RecycleView queue into the RecycleView list scrolling control for display according to the message refresh time interval set by the RecycleView, and assign them to the message model. The message display unit is used to directly scroll and display the complete live message through the SpannableString cache bound to the message model according to a preset scrolling strategy.

9. A computer storage medium, characterized in that, The computer storage medium stores at least one instruction, which, when executed by a processor, implements the message processing method as described in any one of claims 1 to 7.

10. An electronic device, characterized in that, The electronic device includes: a memory and a processor; The memory is used to store at least one instruction; The processor is used to execute the at least one instruction to implement the message processing method as described in any one of claims 1 to 7.