CN106383712B - Toast component implementation method and system - Google Patents

Toast component implementation method and system Download PDF

Info

Publication number
CN106383712B
CN106383712B CN201610837712.8A CN201610837712A CN106383712B CN 106383712 B CN106383712 B CN 106383712B CN 201610837712 A CN201610837712 A CN 201610837712A CN 106383712 B CN106383712 B CN 106383712B
Authority
CN
China
Prior art keywords
toast
layout
component
root
windowmanager
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN201610837712.8A
Other languages
Chinese (zh)
Other versions
CN106383712A (en
Inventor
刘一琛
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Wuhan Douyu Network Technology Co Ltd
Original Assignee
Wuhan Douyu Network Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Wuhan Douyu Network Technology Co Ltd filed Critical Wuhan Douyu Network Technology Co Ltd
Priority to CN201610837712.8A priority Critical patent/CN106383712B/en
Publication of CN106383712A publication Critical patent/CN106383712A/en
Application granted granted Critical
Publication of CN106383712B publication Critical patent/CN106383712B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/20Software design
    • G06F8/24Object-oriented

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • User Interface Of Digital Computer (AREA)
  • Digital Computer Display Output (AREA)

Abstract

The embodiment of the invention provides a method and a system for realizing a Toast component, which are used for solving the technical problem that the prior art depends on availability and authorization when realizing the Toast component. The method comprises the following steps: obtaining a Toast layout; creating a Toast root layout; adding the Toast layout into a Toast root layout through an addView function of the Toast root layout; adding the Toast root layout into an object of the WindowManager through an addView function of the WindowManager; the level of WindowManagerLayoutParams used for representing display view parameters in the addView function of the WindowManager is the same as that of the native Toast; and sending the Toast root layout to the main thread message queue through a Handler object holding the main thread message queue, and further displaying a Toast component corresponding to the Toast root layout. The invention overcomes the defects of the original Toast and avoids the technical problem of relying on the acitvity and the authorization.

Description

Toast component implementation method and system
Technical Field
The invention relates to the technical field of computers, in particular to a method and a system for realizing a Toast component.
Background
The Toast component is a component used in the Android system to display prompt messages on the interface. The original Toast, or native Toast, has the following disadvantages: 1) the display time cannot be defined: the Toast component only has 4 seconds of display time defined by LENGTH _ SHORT (macro definition inside the Toast component) or 5 seconds of display time defined by LENGTH _ LONG (macro definition inside the Toast component), and other display time cannot be set, so that when the Toast display content is too much, the user may not browse the complete content; 2) when a plurality of toasts are displayed, the following steps are displayed in a superposition mode: when a plurality of Toast components are displayed, the Toast components can be overlapped together to cause view overlapping, the overlapped display not only reduces the view rendering efficiency and wastes system resources, but also enables a user to feel obvious frame loss and pause in severe cases; 3) the closing of Toast is inefficient: when an application exits to close a plurality of Toast components, the Toast components are difficult to be saved by using the variable; 4) toast only supports displaying text, not pictures: the interface provided by the native Toast component can only display characters, and a view with one layout is required to be independently customized to be transmitted when a picture is required to be displayed, so that the native Toast layout can be replaced, and the operation is complex; 5) animation showing disappearance cannot be set: the display disappearance animation of the Toast component depends on the display disappearance animation definition of the internal window, and the Toast component does not provide a setting acquisition interface of the internal window, so that the display disappearance animation cannot be set in a user-defined manner; 6) cannot be directly displayed in a common thread: a handler for maintaining messages is arranged in the Toast component, but a thread controlled by the handler is a Toast calling thread without a message queue, so that the Toast component is directly displayed in a common thread without the message queue, and the application is abnormal.
There are two related art techniques in the prior art that address one or more of the problems described above as follows:
1. toast was achieved by Dialog: the problems that the display duration, the picture display, the display disappearing animation setting and the like can be well solved by the Toast component through simulation of the Dialog component provided by the Android. However, since Android display of Dialog must depend on the activity context, when Dialog is displayed, if activity ends, Dialog disappears, and window memory leaks. Meanwhile, in practical development, there is often a need to display Toast across acitvity, so that the 1 st mode cannot display Toast well. In addition, the Dialog supports only Activity level context, and therefore cannot be displayed in service.
2. Toast is realized based on a window of system alert type: android WINDOWs have many different levels, and SYSTEM ALERT type WINDOWs can be displayed at any level of context, but the use of SYSTEM ALERT type WINDOWs must apply for them SYSTEM _ ALERT _ WINDOW permissions. It is not friendly to add extra rights statements for this simple functionality of Taost. In some third-party roms (i.e., customized Android SYSTEMs), the SYSTEM _ ALERT _ WINDOW permission is closed by default, so that a user needs to manually open the permission for an application to display a SYSTEM ALERT type WINDOW, and as a result, the possible devices of some manufacturers cannot be directly displayed, and the user can independently open the permission, so that the user experience is greatly reduced. So the 2 nd approach may necessitate the user to manually initiate permissions for different applications or cause toasts for some applications to not be displayed properly.
Therefore, the prior art has the technical problem of depending on the availability and the authorization when the Toast component is realized.
Disclosure of Invention
The embodiment of the invention provides a method and a system for realizing a Toast component, which are used for solving the technical problem that the prior art depends on availability and authorization when realizing the Toast component.
In a first aspect, the present invention provides a method for implementing Toast component, including:
obtaining a Toast layout;
creating a Toast root layout;
adding the Toast layout into a Toast root layout through an addView function of the Toast root layout;
adding the Toast root layout into an object of the WindowManager through an addView function of the WindowManager; the level of WindowManagerLayoutParams used for representing display view parameters in the addView function of the WindowManager is the same as that of the native Toast;
and sending the Toast root layout to the main thread message queue through a Handler object holding the main thread message queue, and further displaying a Toast component corresponding to the Toast root layout.
Optionally, obtaining a Toast layout comprises:
creating a default Toast layout, or receiving a user-defined Toast layout through a user-defined layout input interface; wherein the default Toast layout comprises at least a sub-control for displaying a picture.
Optionally, before adding the Toast root layout to the object of the WindowManager through the addView function of the WindowManager, the method further includes:
and setting the flag parameter of the WindowManager.LayoutParams to be in a non-clickable state.
Optionally, after sending the Toast root layout to the main thread message queue through a Handler object holding the main thread message queue, the method further includes:
and sending the Toast root layout and the delay time to the main thread message queue through a postDelayed function of the Handler object, and further removing the Toast component after the delay time of the Toast component is displayed.
Optionally, the method further includes:
loading singleton patterns based on lazy loading;
displaying a Toast component corresponding to the Toast root layout, comprising:
judging whether a Windows manager has a single instance of the Toast component or not;
when the single case of the Toast component exists, refreshing the existing single case of the Toast component by the Toast component which is currently read from the main thread message queue;
and when the single case of the Toast component does not exist, creating the single case of the Toast component and displaying the Toast component currently read from the main thread message queue.
In a second aspect, the present invention provides a Toast component implementing system, including:
the layout obtaining module is used for obtaining a Toast layout;
the root layout creating module is used for creating a Toast root layout;
the first adding module is used for adding the Toast layout into the Toast root layout through an addView function of the Toast root layout;
the second adding module is used for adding the Toast root layout into an object of the WindowManager through an addView function of the WindowManager; the level of WindowManagerLayoutParams used for representing display view parameters in the addView function of the WindowManager is the same as that of the native Toast;
and the message sending module is used for sending the Toast root layout to the main thread message queue through a Handler object holding the main thread message queue, and further displaying a Toast component corresponding to the Toast root layout.
Optionally, the layout obtaining module is configured to create a default Toast layout, or receive a custom Toast layout through a custom layout input interface; wherein the default Toast layout comprises at least a sub-control for displaying a picture.
Optionally, the system further includes:
the setting module is used for setting a flag parameter of the Windows manager, LayoutParams to be in a non-clickable state before the Toast root layout is added to the object of the Windows manager through an addView function of the Windows manager.
Optionally, the message sending module is further configured to send the root layout and the delay time of the Toast to the main thread message queue through a postDelayed function of a Handler object after the Handler object holding the main thread message queue sends the root layout and the delay time of the Toast to the main thread message queue, and then remove the Toast after the delay time of the Toast is displayed.
Optionally, the system further includes:
the loading module is used for loading the singleton mode based on lazy loading;
the display module is used for judging whether the Windows manager has a single instance of the Toast component or not; when the single case of the Toast component exists, refreshing the existing single case of the Toast component by the Toast component which is currently read from the main thread message queue; when the singleton of the Toast component does not exist, creating the singleton of the Toast component and displaying the Toast component currently read from the main thread message queue
One or more technical solutions in the embodiments of the present application have at least one or more of the following technical effects:
in the technical scheme of the embodiment of the invention, firstly, a Toast layout is obtained and a root layout is created, then, the Toast root layout is added into an object of a Windows manager through an addView function of the Windows manager, finally, a handler object with a main thread message queue sends the Toast root layout to the main thread message queue, and when the main thread processes a Toast root layout message, a corresponding Toast component can be displayed. The addView function of the WindowManager has the same level as the native Toast, and the WindowManager. On one hand, because the Toast component of the embodiment of the invention is realized by the Toast layout, and is not simulated by other objects, the display of the Toast does not depend on the accessibility, and the operation across the accessibility does not cause the display of the Toast to be abnormal; on the other hand, because the native Toast does not need to be displayed with permission, and the addView function of the WindowManager in the embodiment of the present invention, which is used for representing the display view parameters, is set to the same level as the native Toast, the application of permission is naturally not needed when the Toast component of the present invention is displayed, so that the display abnormality of the Toast component due to the lack of permission is avoided.
Drawings
FIG. 1 is a flow chart of a method for implementing a Toast component according to an embodiment of the present invention;
FIG. 2 is a logic flow diagram of a Toast component implementation in an embodiment of the present invention;
fig. 3 is a schematic diagram of a system architecture for implementing the Toast component in the embodiment of the present invention.
Detailed Description
The embodiment of the invention provides a method and a system for realizing a Toast component, which are used for solving the technical problem that the prior art depends on availability and authorization when realizing the Toast component.
In order to solve the technical problems, the technical scheme provided by the invention has the following general idea:
in the technical scheme of the embodiment of the invention, firstly, a Toast layout is obtained and a root layout is created, then, the Toast root layout is added into an object of a Windows manager through an addView function of the Windows manager, finally, a handler object with a main thread message queue sends the Toast root layout to the main thread message queue, and when the main thread processes a Toast root layout message, a corresponding Toast component can be displayed. The addView function of the WindowManager has the same level as the native Toast, and the WindowManager. On one hand, because the Toast component of the embodiment of the invention is realized by the Toast layout, and is not simulated by other objects, the display of the Toast does not depend on the accessibility, and the operation across the accessibility does not cause the display of the Toast to be abnormal; on the other hand, because the native Toast does not need to be displayed with permission, and the addView function of the WindowManager in the embodiment of the present invention, which is used for representing the display view parameters, is set to the same level as the native Toast, the application of permission is naturally not needed when the Toast component of the present invention is displayed, so that the display abnormality of the Toast component due to the lack of permission is avoided.
The technical solutions of the present invention are described in detail below with reference to the drawings and specific embodiments, and it should be understood that the specific features in the embodiments and examples of the present invention are described in detail in the technical solutions of the present application, and are not limited to the technical solutions of the present application, and the technical features in the embodiments and examples of the present application may be combined with each other without conflict.
The term "and/or" herein is merely an association describing an associated object, meaning that three relationships may exist, e.g., a and/or B, may mean: a exists alone, A and B exist simultaneously, and B exists alone. In addition, the character "/" herein generally indicates that the former and latter related objects are in an "or" relationship.
A first aspect of the present invention provides a method for implementing a Toast component, please refer to fig. 1, which is a flowchart of the method for implementing a Toast component. The method comprises the following steps:
s101: obtaining a Toast layout;
s102: creating a Toast root layout;
s103: adding the Toast layout into a Toast root layout through an addView function of the Toast root layout;
s104: adding the Toast root layout into an object of the WindowManager through an addView function of the WindowManager;
s105: and sending the Toast root layout to the main thread message queue through a Handler object holding the main thread message queue, and further displaying a Toast component corresponding to the Toast root layout.
The method in the embodiment of the present invention may be applied to electronic devices such as smart phones, computers, smart watches, and the like, as long as an operating system of the electronic device includes an Android system, and the present invention is not particularly limited.
The Toast component is a component provided by the Android system and used for displaying messages on an interface in a lightweight mode.
The method for implementing the Toast component in the embodiment of the present invention will be described with reference to the logic flow diagram shown in fig. 2.
In S101, first, a Toast layout needs to be obtained. Specifically, obtaining the Toast layout in the embodiment of the present invention includes:
creating a default Toast layout, or receiving a user-defined Toast layout through a user-defined layout input interface; wherein the default Toast layout comprises at least a sub-control for displaying a picture.
Specifically, if a Toast layout is created by a manager, there are two ways to create the Toast layout in the Android, one is to design the layout style by editing a layout file through XML (Extensible markup language), and the other is to create the layout through code. In the specific implementation process, a person skilled in the art to which the present invention pertains may select according to the practice, and the present invention is not particularly limited.
The introduction is made by way of example through XML creation. Firstly, editing a default Toast layout by using XML according to design requirements. In the embodiment of the present invention, in order to enable the Toast to directly display the picture, when creating the default Toast layout, at least a child control ImageView for displaying the picture should be created, and the ImageView should be included in the default Toast layout. Of course, in a specific implementation process, TextView for displaying text may be further included in the default Toast layout. Wherein, the ImageView and the TextView are both View views.
Therefore, through the 2 child controls ImageView and TextView of the default Toast layout, a manager can arbitrarily set the pictures and texts to be displayed by the Toast component as required, so that the Toast component not only supports text display, but also supports picture display.
In addition, the Android system of the embodiment of the invention also provides an interface for transmitting the custom Toast layout externally, and the system can receive the custom Toast layout transmitted from the outside through the custom layout transmitting interface.
Therefore, the system may use the received custom Toast layout as the Toast layout, and may also obtain a self-created default Toast layout as the Toast layout, which is not specifically limited in the present invention. Specifically, as shown in fig. 2, in S201, it is determined whether there is a custom layout, and if there is no custom layout, S202 is executed: a layout containing ImageView and TextView, i.e., a default Toast layout, is created.
In S102, a Toast root layout is created. In particular, the root layout is created directly by the code. In the embodiment of the present invention, it is assumed that a FrameLayout object is created directly using FrameLayout class as root layout rootview of the Toast, as shown in S203 of fig. 2: a FrameLayout object rootview is created. Through the management of the root layout rootview of the Toast, the display content of the whole Toast component can be directly managed conveniently. FrameLayout is one of Android layout controls, and FrameLayout can wrap other controls, wherein the wrapped controls are called child controls of FrameLayout.
In a specific implementation process, the execution sequence of obtaining the Toast layout in S101 and creating the Toast root layout in S102 may be that S101 is before, S102 is after, or S102 is before, S101 is after, or S101 and S102 may be executed simultaneously, which is not limited in the present invention.
Next, after S101 and S102 are executed, in S103, the Toast layout is added to the Toast root layout by the addView function of the Toast root layout.
Specifically, in the embodiment of the present invention, in S201, it is determined whether there is a custom layout, and if yes, that is, the imported custom layout object is not null, the custom Toast layout is added to the rootview through the addView function of the FrameLayout object; if not, namely the user-defined layout object is null, adding the default Toast layout into the rootview through the addView function of the FrameLayout object.
In an embodiment of the present invention, the addview function receives a view as a parameter. For convenience of introduction, a Toast layout added to a Toast root layout is referred to as a child view, regardless of whether the Toast layout added to the Toast root layout is specifically a default Toast layout or a custom Toast layout.
In addition, since the WindowManager (manager of window in Android system, used for controlling creation, destruction, and update of window) only supports the display disappearing Animation edited by XML file, and does not support Animation (Animation is a component used by Android to process Animation) class object created by code. In order to enable the Toast component to support Animation, the Animation for setting Toast is realized by setting Animation for rooview.
Because all views of the Toast component are inside the rootview, the animation processing of the rootview is just to perform the animation processing of the whole Toast component. And the View of the Android system directly supports Animation, so that a user-defined display disappearance Animation can be obtained between S203 and S204 through an interface which is externally provided by the rootview and is used for setting the display disappearance Animation. Therefore, the user-defined Toast component display disappearance animation can be realized through the external setting interface of the rootview.
Furthermore, in S204, an addview method of rootview is called, that is, the setup animation and childview are added to the root layout of the Toast through the addview function in the framelayout object, and the display layout setup of the Toast component is completed.
As can be seen from the above description, in the embodiment of the present invention, the Toast display view is created in a manner that the Android system itself creates the Toast component, and is not simulated by other objects, such as dialogs, so that the display of the Toast component in the embodiment of the present invention does not depend on activity. Then, the operation across activity does not result in the disappearance of the Toast component in the application.
Next, after the setting of the display layout is completed, the Toast component needs to be displayed on the interface. In S104, the Toast root layout is added to the object of the WindowManager by the addView function of the WindowManager.
Specifically, all views displayed in the Android system are managed through a Windows manager, objects of the Windows manager can be acquired through a function getSystenservice provided by the system, the objects of the Windows manager are obtained, then addview functions of the objects of the Windows manager are called to add rootviews, and the views added through the addview functions can be displayed on an Android interface.
Therefore, in S205, getSystenService acquires a WindowManager object, and then, in S207, addview of WindowManager is called.
In S206 before S207, windows manager.
Specifically, addview of the WindowManager has two parameters, the first is view to be displayed, which is rootview in the embodiment of the present invention; the second is windowmanager. Layoutparams is a technique for representing display view parameters such as coordinate position of the display view, width and height of the view, etc.
In the same Android screen, a high-level view is overlaid on a low-level view. Too high a level requires the SYSTEM _ ALERT _ WINDOW right to be obtained before it can be displayed. In order to solve the technical problem that the Toast component can be displayed only when the permission is required to be applied in the prior art, in the embodiment of the invention, the value of the Type parameter representing the level in the Windows manager.
In addition, before S104, S206 further includes setting a Flags parameter of windowmanager, layout params to an clickable state. Specifically, to avoid consuming device resources without end, it is also necessary to set the Flags parameter of windowmanager. In a specific implementation process, Flags can be further set as Toast which does not display and has no focus under the screen locking condition. Those skilled in the art to which the present invention pertains may set the present invention according to the practice, and the present invention is not particularly limited.
In a specific implementation process, the execution sequence of S205 and S206 is not sequential, and a person skilled in the art to which the present invention belongs may arbitrarily set the execution sequence as needed.
By setting the layout parameters and creating the view, the view can be directly displayed on a mobile phone screen by addview of the Windows manager, but direct display has a problem, and Android view updating operation is not thread-safe. Thread safety refers to that under the condition of multi-thread coding, if two threads access the same resource at the same time, the state of the resource has uncertainty (for example, the two threads perform write operation simultaneously, the result is uncertain), and thread safety is to ensure that the result has uniqueness, for example, a certain thread accesses first, a certain thread accesses later, and the like. If the view system were to be displayed directly in an asynchronous thread (i.e., a thread other than the main thread), an exception would be thrown, causing the application to hang.
To solve the problem of display in a normal thread, a Handler object holding a main thread message queue needs to be created first. A Handler is a system component that controls messages and can send messages to specified threads. Clicking events and drawing events in the Android can be regarded as messages, the messages are put into a queue by the system to be processed in sequence, and meanwhile, a message queue of the system can be created through a Looper component. Looper is a component used to create message queues and message loops.
Specifically, in S208, a holding main thread message queue Handler is created by looper. Specifically, the main thread message queue is obtained through a looper, getMainLoopper, and then a Handler object is created by taking the main thread message queue as a parameter, so that the created Handler holds the main thread message queue. Then all messages sent through the Handler are sent to the main thread message queue and run on the main thread. Specifically, the Handler sends addview of the WindowManager to the main thread (not shown in fig. 2) through the post function of the Handler, and further sends rootview to the main thread. The basic code at the time of transmission is, for example
Post (new Runable () {// send a message to the thread held by Handler
AddView (rootview, LayoutParams)// created view displays to interface
});
Finally, when the main thread reads the message displayed by the Toast component, the operation in S209 is executed, that is, the Toast component is displayed.
Further, since the duration of Toast display in the prior art is only 4 seconds defined by LENGTH _ SHORT (macro definition inside Toast component) and 5 seconds defined by LENGTH _ LONG (macro definition inside Toast component). In order to control the display duration of the Toast component, after S105 in the embodiment of the present invention, the method may further include:
and sending the Toast root layout and the delay time to the main thread message queue held by the Handler object through a postDelayed function of the Handler object, further displaying a Toast component corresponding to the Toast root layout, and removing the Toast component after the delay time of the Toast component is displayed.
Specifically, the postDelayed function and the post function both have the same function and are used for transmitting messages into a message queue held by a Handler object, but the postDelayed function supports more than the post function by a parameter, namely delay time. When the thread reads the postDelayed send message, the corresponding operation will be executed by the delay time. Therefore, after the post function sends the rootview to the main thread, the function delayed execution of the postDelayed of the Handler is used for entering the delay parameter time in S210, and the delay time and the rootview set as required are sent to the remove of the WindowManager. After the main thread reads the message and waits for the Toast component to display the delay time, remove the Toast component by WindowManager in S211. Finally, the Toast component is turned off in S212.
The basic code at the time of transmission is, for example
Postdelayed (new Runable () {// resending the message delay time to the holding thread
Remove view from interface display
},time);
Therefore, through the process, the display duration of the Toast component can be set arbitrarily.
In the specific implementation process, a person having ordinary skill in the art to which the present invention belongs may select post transmission or PostDelayed transmission as needed, and the present invention is not particularly limited.
Further, the method in the embodiment of the present invention may further include:
loading singleton patterns based on lazy loading;
displaying a Toast component corresponding to the Toast root layout, comprising:
judging whether a Windows manager has a single instance of the Toast component or not;
when the single case of the Toast component exists, refreshing the existing single case of the Toast component by the Toast component which is currently read from the main thread message queue;
and when the single case of the Toast component does not exist, creating the single case of the Toast component and displaying the Toast component currently read from the main thread message queue.
Specifically, multiple Toast objects are created when multiple calls are made, the multiple Toast objects are overlapped to influence user experience and system resources, and a singleton mode is introduced to solve the problem and ensures that only one object exists in one class in the same process. That is to say, in the same process, the use of Toast is the same object anywhere, so that the Toast overlapping condition does not occur. When the Toast component is currently being displayed, if the Toast component is called again, the Toast component is not recreated, but the content is directly toasted, and the display time of the Toast is reset. The conventional use of the singleton mode is that a class holds a unique object for a long time, and in the embodiment of the invention, in order to save system resources, singleton is realized through lazy loading. Then, when displaying the Toast component, firstly, whether the WindowManager has a single instance of the Toast component is judged. If yes, directly refreshing the existing single instance of the Toast component by the Toast component read from the main thread message queue; if not, creating a single instance of the Toast component and displaying the Toast component currently read from the main thread message queue.
It can be seen from the above description that only one Toast component is displayed in the main thread through the single-case mode, so that at most one Toast component is displayed in the same unit time, and therefore, the memory waste and the interface blockage caused by the overlapping display of a plurality of Toast components when a user performs a large number of repeated operations are avoided. And, because there is only one Toast component, when shutting down the Toast component, it does not need to save through the variable, but can be shut down directly anywhere.
Through the technical scheme, the display time of the Toast assembly can be set at will, only one Toast is displayed, and a plurality of toasts cannot be displayed in a superposition mode. And, when closing Toast, it is no longer necessary to save all Toast variables. The Toast can also support picture display through the ImageView, the display disappearance animation of the Toast component can be set through the animation setting interface, and the display disappearance animation can be displayed in any thread. Therefore, while the present invention solves the above-mentioned drawbacks of the plurality of toasts, on one hand, since the Toast components of the embodiments of the present invention are implemented by the Toast layout, the display of the Toast is not dependent on the availability; on the other hand, by setting the level of windowmanager, layout params, which is the same as that of the native Toast, the display of the Toast component of the present invention avoids that the Toast component displays an exception because it does not acquire an authority.
Based on the same inventive concept as the Toast component implementing method in the foregoing embodiment, a second aspect of the present invention further provides a Toast component implementing system, as shown in fig. 3, including:
a layout obtaining module 101, configured to obtain a Toast layout;
a root layout creation module 102, configured to create a Toast root layout;
a first adding module 103, configured to add the Toast layout to a Toast root layout through an addView function of the Toast root layout;
a second adding module 104, configured to add the Toast root layout to an object of the WindowManager through an addView function of the WindowManager; the level of WindowManagerLayoutParams used for representing display view parameters in the addView function of the WindowManager is the same as that of the native Toast;
and the message sending module 105 is configured to send the Toast root layout to the main thread message queue through a Handler object holding the main thread message queue, and further display a Toast component corresponding to the Toast root layout.
The layout obtaining module 101 is configured to create a default Toast layout, or receive a custom Toast layout through a custom layout input interface; wherein the default Toast layout comprises at least a sub-control for displaying a picture.
Further, the system further comprises:
the setting module is used for setting a flag parameter of the Windows manager, LayoutParams to be in a non-clickable state before the Toast root layout is added to the object of the Windows manager through an addView function of the Windows manager.
Further, the message sending module 105 is further configured to send the root layout and the delay time of the Toast to the main thread message queue through a postDelayed function of the Handler object after sending the root layout to the main thread message queue through the Handler object holding the main thread message queue, and further remove the Toast component after displaying the delay time of the Toast component.
Still further, the system further comprises:
the loading module is used for loading the singleton mode based on lazy loading;
the display module is used for judging whether the Windows manager has a single instance of the Toast component or not; when the single case of the Toast component exists, refreshing the existing single case of the Toast component by the Toast component which is currently read from the main thread message queue; and when the single case of the Toast component does not exist, creating the single case of the Toast component and displaying the Toast component currently read from the main thread message queue.
Various changes and specific examples of the Toast component implementing method in the foregoing embodiments of fig. 1-2 are also applicable to the Toast component implementing system in the present embodiment, and through the foregoing detailed description of the Toast component implementing method, those skilled in the art can clearly know the implementing method of the Toast component implementing system in the present embodiment, so for the brevity of the description, detailed descriptions are omitted here.
One or more technical solutions in the embodiments of the present application have at least one or more of the following technical effects:
in the technical scheme of the embodiment of the invention, firstly, a Toast layout is obtained and a root layout is created, then, the Toast root layout is added into an object of a Windows manager through an addView function of the Windows manager, finally, a handler object with a main thread message queue sends the Toast root layout to the main thread message queue, and when the main thread processes a Toast root layout message, a corresponding Toast component can be displayed. The addView function of the WindowManager has the same level as the native Toast, and the WindowManager. On one hand, because the Toast component of the embodiment of the invention is realized by the Toast layout, and is not simulated by other objects, the display of the Toast does not depend on the accessibility, and the operation across the accessibility does not cause the display of the Toast to be abnormal; on the other hand, because the native Toast does not need to be displayed with permission, and the addView function of the WindowManager in the embodiment of the present invention, which is used for representing the display view parameters, is set to the same level as the native Toast, the application of permission is naturally not needed when the Toast component of the present invention is displayed, so that the display abnormality of the Toast component due to the lack of permission is avoided.
As will be appreciated by one skilled in the art, embodiments of the present invention may be provided as a method, system, or computer program product. Accordingly, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present invention may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The present invention is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each flow and/or block of the flow diagrams and/or block diagrams, and combinations of flows and/or blocks in the flow diagrams and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
It will be apparent to those skilled in the art that various changes and modifications may be made in the present invention without departing from the spirit and scope of the invention. Thus, if such modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include such modifications and variations.

Claims (8)

1. A Toast component implementation method is characterized by comprising the following steps:
obtaining a Toast layout, which specifically comprises the following steps: judging whether a custom Toast layout received through a custom layout incoming interface exists or not, if not, creating a default Toast layout, wherein the Toast layout comprises a default Toast layout of a sub-control ImageView used for displaying pictures and a sub-control TextView used for displaying characters;
directly creating a Toast root layout through codes;
adding the Toast layout into a Toast root layout through an addView function of the Toast root layout;
adding the Toast root layout into an object of the WindowManager through an addView function of the WindowManager; the level of WindowManager, layout params used for representing display view parameters in the addView function of the WindowManager is the same as that of the native Toast, and the method specifically includes the following steps: setting the value of the Type parameter representing the grade in WindowManager.LayoutParams to be consistent with the native Toast;
and sending the Toast root layout to the main thread message queue through a Handler object holding the main thread message queue, and further displaying a Toast component corresponding to the Toast root layout.
2. The method of claim 1, prior to adding the Toast root layout to the object of the WindowManager via its addView function, further comprising:
and setting the flag parameter of the WindowManager.LayoutParams to be in a non-clickable state.
3. The method of claim 1, after sending the Toast root layout into a main-thread message queue through a Handler object holding the main-thread message queue, further comprising:
and sending the Toast root layout and the delay time to the main thread message queue through a postDelayed function of the Handler object, and further removing the Toast component after the delay time of the Toast component is displayed.
4. The method of claim 1, wherein the method further comprises:
loading singleton patterns based on lazy loading;
displaying a Toast component corresponding to the Toast root layout, comprising:
judging whether a Windows manager has a single instance of the Toast component or not;
when the single case of the Toast component exists, refreshing the existing single case of the Toast component by the Toast component which is currently read from the main thread message queue;
and when the single case of the Toast component does not exist, creating the single case of the Toast component and displaying the Toast component currently read from the main thread message queue.
5. A Toast component implementation system, comprising:
the layout obtaining module is used for obtaining a Toast layout, and specifically comprises:
judging whether a custom Toast layout received through a custom layout incoming interface exists or not, if not, creating a default Toast layout, wherein the Toast layout comprises a default Toast layout of a sub-control ImageView used for displaying pictures and a sub-control TextView used for displaying characters;
the first adding module is used for adding the Toast layout into the Toast root layout through an addView function of the Toast root layout;
the second adding module is used for adding the Toast root layout into an object of the WindowManager through an addView function of the WindowManager; the level of WindowManager, layout params used for representing display view parameters in the addView function of the WindowManager is the same as that of the native Toast, and the method specifically includes the following steps: setting the value of the Type parameter representing the grade in WindowManager.LayoutParams to be consistent with the native Toast;
and the message sending module is used for sending the Toast root layout to the main thread message queue through a Handler object holding the main thread message queue, and further displaying a Toast component corresponding to the Toast root layout.
6. The Toast component implementing system according to claim 5, wherein the system further comprises:
the setting module is used for setting a flag parameter of the Windows manager, LayoutParams to be in a non-clickable state before the Toast root layout is added to the object of the Windows manager through an addView function of the Windows manager.
7. The Toast component implementing system according to claim 5, wherein the message sending module is further configured to send the Toast root layout and the delay time to the main thread message queue through a postDelayed function of a Handler object after sending the Toast root layout to the main thread message queue through the Handler object holding the main thread message queue, and further to remove the Toast component after displaying the delay time of the Toast component.
8. The Toast component implementing system according to claim 5, wherein the system further comprises:
the loading module is used for loading the singleton mode based on lazy loading;
the display module is used for judging whether the Windows manager has a single instance of the Toast component or not; when the single case of the Toast component exists, refreshing the existing single case of the Toast component by the Toast component which is currently read from the main thread message queue; and when the single case of the Toast component does not exist, creating the single case of the Toast component and displaying the Toast component currently read from the main thread message queue.
CN201610837712.8A 2016-09-21 2016-09-21 Toast component implementation method and system Active CN106383712B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201610837712.8A CN106383712B (en) 2016-09-21 2016-09-21 Toast component implementation method and system

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201610837712.8A CN106383712B (en) 2016-09-21 2016-09-21 Toast component implementation method and system

Publications (2)

Publication Number Publication Date
CN106383712A CN106383712A (en) 2017-02-08
CN106383712B true CN106383712B (en) 2020-03-17

Family

ID=57935802

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201610837712.8A Active CN106383712B (en) 2016-09-21 2016-09-21 Toast component implementation method and system

Country Status (1)

Country Link
CN (1) CN106383712B (en)

Families Citing this family (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106941568A (en) * 2017-03-30 2017-07-11 努比亚技术有限公司 A kind of Toast pop-ups optimization display methods, device and Toast pop-ups dispatching method and device
CN107122186B (en) * 2017-04-25 2020-06-09 南京秦淮紫云创益企业服务有限公司 Management method and management device for floating window
CN110018813B (en) * 2018-01-10 2022-12-02 武汉斗鱼网络科技有限公司 Toast design method, storage medium, electronic device and system
CN108287742B (en) * 2018-01-10 2020-08-04 武汉斗鱼网络科技有限公司 Method, storage medium, electronic device and system for realizing floating layer animation
CN110457132B (en) * 2019-08-01 2022-08-05 武汉斗鱼鱼乐网络科技有限公司 Method and device for creating functional object and terminal equipment
CN112527443A (en) * 2020-12-28 2021-03-19 平安普惠企业管理有限公司 Prompt box display method and device, electronic equipment and computer readable storage medium
CN113342548A (en) * 2021-06-22 2021-09-03 平安普惠企业管理有限公司 Message notification method, device, equipment and storage medium based on Toast
CN116088714B (en) * 2022-05-26 2024-05-28 荣耀终端有限公司 Message prompt box processing method and related device

Family Cites Families (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US10705692B2 (en) * 2009-05-21 2020-07-07 Sony Interactive Entertainment Inc. Continuous and dynamic scene decomposition for user interface
CN104572110A (en) * 2015-01-19 2015-04-29 山东大学 Optimizing method of Toast activeX in android system

Non-Patent Citations (3)

* Cited by examiner, † Cited by third party
Title
Android中通过反射来设置Toast的显示时间;weixin_30719711;《https://blog.csdn.net/jiangwei0910410003/article/details/》;20140511;第1-17页 *
android设计模式之单例模式详解;佚名;《https://www.jb51.net/article/49221.htm》;20140422;第1-6页 *
weixin_30719711.Android中通过反射来设置Toast的显示时间.《https://blog.csdn.net/jiangwei0910410003/article/details/》.2014,第1-17页. *

Also Published As

Publication number Publication date
CN106383712A (en) 2017-02-08

Similar Documents

Publication Publication Date Title
CN106383712B (en) Toast component implementation method and system
US9354856B2 (en) Software installation method and device
US9792354B2 (en) Context aware user interface parts
US8943609B2 (en) Apparatus and method for configuring password and for releasing lock
CN103914450A (en) Method and device for presenting web graphics in mobile terminal platform
KR20140030226A (en) Global composition system
CN108304234B (en) Page display method and device
US9286142B2 (en) Methods and systems for supporting a rendering API using a runtime environment
CN105208442A (en) Video playing method of video playing application program and apparatus thereof
CN109840115A (en) RecyclerView paging loading method and device in a kind of android system
CN110750255A (en) Applet rendering method and device
CN110110306B (en) Data display method and device, terminal and computer readable storage medium
US20150067664A1 (en) System, method and apparatus for transparently enabling software applications with adaptive user interfaces
CN111367518A (en) Page layout method and device, computing equipment and computer storage medium
CN107291340B (en) Method for realizing interface effect, computing equipment and storage medium
CN107066872B (en) Plug-in right control method and device and plug-in system
CN107729107B (en) Modal dialog box processing method and device
CN109819329B (en) Window display method and smart television
CN107577454A (en) A kind of application program multithreading running optimizatin method based on python
CN109800044B (en) HTML5 double-screen application method and device and electronic equipment
CN111324347A (en) Component construction method, device and system, page generation method, equipment and medium
CN108632069B (en) Client configuration method, system and related equipment
CN115129444A (en) Application program display method and device and computer readable storage medium
CN113626000A (en) Method and device for automatically switching page layout according to access mode
CN104572199B (en) The method and apparatus that a kind of No starting item starts

Legal Events

Date Code Title Description
C06 Publication
PB01 Publication
C10 Entry into substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant