CN110018813B - Toast design method, storage medium, electronic device and system - Google Patents

Toast design method, storage medium, electronic device and system Download PDF

Info

Publication number
CN110018813B
CN110018813B CN201810023297.1A CN201810023297A CN110018813B CN 110018813 B CN110018813 B CN 110018813B CN 201810023297 A CN201810023297 A CN 201810023297A CN 110018813 B CN110018813 B CN 110018813B
Authority
CN
China
Prior art keywords
thread
toast
sub
content
display
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
CN201810023297.1A
Other languages
Chinese (zh)
Other versions
CN110018813A (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 CN201810023297.1A priority Critical patent/CN110018813B/en
Publication of CN110018813A publication Critical patent/CN110018813A/en
Application granted granted Critical
Publication of CN110018813B publication Critical patent/CN110018813B/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
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Stored Programmes (AREA)
  • Devices For Executing Special Programs (AREA)

Abstract

The invention discloses a Toast design method, a storage medium, electronic equipment and a system, which relate to the field of mobile interconnection and comprise the steps of defining a class and defining an object acquisition method in the defined class, wherein the object acquisition method is used for acquiring the defined class by a current thread; defining a context object in the constructor of the defined class; when Toast display is needed, the defined class is acquired by a current thread, then whether the current thread is a main thread or a sub-thread is judged, if yes, a system Toast object is constructed based on a context object in a construction function of the defined class to display Toast content; if the thread is the sub thread, the sub thread sends an empty message to the message loop of the main thread through the tool Handler, and the main thread receives the message and calls the main thread to display the Toast content. The method and the system can expand the compatibility of the main process and the sub-process of the system for Toast display.

Description

Toast design method, storage medium, electronic device and system
Technical Field
The invention relates to the field of mobile interconnection, in particular to a Toast design method, a storage medium, electronic equipment and a system.
Background
In the Android development process, toast is a common prompt control and is used for prompting information. However, in the actual application process, the display use of Toast has the defect that Toast must be used in a main thread, and if Toast is used in a sub-thread, system operation is abnormal, so that Toast has great limitation.
Disclosure of Invention
Aiming at the defects in the prior art, the invention aims to provide a method for designing Toast, which is used for expanding the compatibility of a main process and a subprocess of a system for Toast display.
In order to achieve the above purposes, the technical scheme adopted by the invention is as follows:
defining a class, and defining an object acquisition method in the defined class, wherein the object acquisition method is used for acquiring the defined class by the current thread;
defining a context object in the constructor of the defined class;
when Toast display is needed, the defined class is obtained by the current thread, then the current thread is judged to be a main thread or a sub-thread, if the current thread is the main thread, a system Toast object is constructed based on a context object in a constructor of the defined class to display Toast content; if it is a child thread, then
And the sub-thread sends an empty message to the message loop of the main thread through the tool Handler, and the main thread receives the message and calls the main thread to display the Toast content.
On the basis of the technical proposal, the device comprises a shell,
the object acquisition method is a public method, and the object acquisition method is declared to be a static method.
The context object is application-level.
On the basis of the technical scheme, when the current thread is a sub-thread, the sub-thread sends an empty message to the message loop of the main thread through the tool type Handler, the main thread receives the message, and then a system Toast object is constructed based on the context object in the constructor of the definition class to display Toast content.
On the basis of the technical scheme, the judgment that the current thread is a main thread or a sub-thread specifically comprises the following steps:
defining an abnormal capture code block;
popping a Toast with empty content and transparent background in the abnormal capturing code block;
if the content is empty and the popup of the transparent background Toast is successfully executed, the current thread is a main thread, and if the content is empty and the popup of the transparent background Toast is failed to be executed, the current thread is a sub thread.
On the basis of the technical scheme, a display method is further defined in the defined class, and after the construction of a system Toast object is completed based on the context object in the constructor of the defined class, the Toast content of the constructed system Toast object is displayed through the display method.
The invention also provides a storage medium having stored thereon a computer program which, when executed by a processor, implements the method described above.
The invention also provides an electronic device, which comprises a memory and a processor, wherein the memory stores a computer program running on the processor, and the processor executes the computer program to realize the method.
The invention also provides a Toast design system, comprising:
the definition module is used for defining a class, defining an acquisition object method in the defined class and defining a context object in a constructor of the defined class, wherein the acquisition object method is used for acquiring the defined class by a current thread;
the judging module is used for acquiring the defined class by the current thread when the Toast display is required, and then judging whether the current thread is a main thread or a sub-thread;
the execution module is used for constructing a system Toast object based on the context object in the constructor of the definition class to display Toast content if the current thread is a main thread; if the thread is the sub thread, the sub thread sends an empty message to the message loop of the main thread through the tool Handler, and the main thread receives the message and calls the main thread to display the Toast content.
On the basis of the technical scheme, the object obtaining method is a public method, the object obtaining method is declared to be a static method, and the context object is at an application level.
On the basis of the technical scheme, when the current thread is a sub-thread, the sub-thread sends an empty message to the message loop of the main thread through the tool type Handler, the main thread receives the message, and then a system Toast object is constructed based on the context object in the constructor of the definition class to display Toast content.
Compared with the prior art, the invention has the advantages that: defining a class, defining an object obtaining method in the defined class, obtaining the defined class by a current thread when the Toast display is needed, judging whether the current thread is a main thread or a sub-thread, and if the current thread is the main thread, constructing a system Toast object based on a context object in a constructor of the defined class to display Toast content; if the system is a sub-thread, the sub-thread sends an empty message to the message loop of the main thread through the tool Handler, the main thread receives the message and calls the main thread to display the Toast content, display of the main thread and the sub-thread on the Toast is achieved, compatibility of the system on the Toast display is expanded, and development efficiency is greatly improved.
Drawings
FIG. 1 is a flow chart of a Toast designing method according to an embodiment of the present invention;
fig. 2 is a schematic structural diagram of an electronic device according to an embodiment of the present invention.
Detailed Description
The present invention will be described in further detail with reference to the accompanying drawings and examples.
Referring to fig. 1, an embodiment of the present invention provides a method for designing Toast, which is suitable for an Android platform, and solves the compatibility problem of system Toast display, so that Toast can be used in any thread. The Toast design method of the embodiment of the invention specifically comprises the following steps:
s1: defining a class, and defining an acquisition object method in the defined class, wherein the acquisition object method is used for acquiring the defined class by the current thread. The name of the class defined in the embodiment of the invention is DYTOAST. The method for obtaining the object is a public method, and declares the method for obtaining the object as a static method, and the method for obtaining the object specifically comprises the following steps:
public static DYToast getDYToastInstance()
the method for obtaining the object mainly facilitates other classes or obtains DYTOAST when calling, the method for obtaining the object is a public method which is a public type and can be called by other classes, and then the method is declared to be a static method which can be called directly through the classes without instantiation operation on the classes. Where DYTOAST getDYTOAStInstance () indicates that the function name is getDYTOAStInstance, the function has no parameter input, and the function will return a DYTOAST object when used.
S2: a context object is defined in the constructor of the defined class and is application-level. For a defined class of DYTOAST, the constructor of DYTOAST is
public void DYToast();
The construction method is a return void (null return value), has no parameters, and is a function whose function name is the same as the class name. The display of Toast needs to obtain a context object, in Android, the context has two types, one of which is page-level context, usually the context object in Activity, which is one of the most basic and most commonly used four components in the Android component; the second is Application-level context, typically the context object in an Application. When the toast is displayed, a context object is needed, if the page-level context object is used, information related to the page needs to be acquired when the toast is displayed every time, the acquisition of the page information is time-consuming and can cause element information of the page held by the DYTOAST class, and the risk that the page cannot be released is caused. Therefore, the context object at the application level is selected, and is the context at the application level, so that the context does not cause memory leakage even if not released, and the acquisition efficiency of the context at the application level is more efficient.
After a Context object is defined in a constructor of a defined class, initializing the Context object in a constructor of DYTOAST, wherein a specific initialization method is to assign the Context object by calling a getapplicationContext method in Application, so that a DYTOAST class has one Context object, and the Context object can be used when the toast is displayed at the later stage.
S3: when Toast display is needed, the defined class is acquired by a current thread, then whether the current thread is a main thread or a sub-thread is judged, if yes, a system Toast object is constructed based on a context object in a construction function of the defined class to display Toast content; if it is a child thread, S4 is entered.
For the judgment of whether the current thread is the main thread or the sub-thread, specifically:
s301: defining an abnormal capture code block;
s302: popping a Toast with empty content and transparent background in the abnormal capturing code block;
s303: if the content is empty and the popup of the transparent background Toast is successfully executed, the current thread is a main thread, and if the content is empty and the popup of the transparent background Toast is failed to be executed, the current thread is a sub thread.
In Android, for the judgment of whether a Thread is a main Thread or a sub-Thread, a related class is a Thread class, a static method currentThread method is provided in the class, the method returns the name of the current Thread, the name is String-type character String information, the name information is represented as the main Thread if the name is main under the conventional condition, but some differentiated platforms return information such as main1 or main-1 and the like to represent as the main Thread, so that the judgment is carried out only by the name method, and the possibility of wrong judgment is caused.
Therefore, the embodiment of the invention defines the mode of the abnormal capture code block and directly pops up the toast content with empty content and transparent background in the abnormal capture code block. Because the popped content is empty and the background of the popped toast is completely transparent, the condition that the user cannot view the toast content is ensured, and the imperceptibility of the user is also ensured. If the Toast normal execution is successful, the current thread is determined to be the main thread, and if the Toast pop execution fails, the method executes an exception capture code block, and if an exception is captured in the code block, the current thread is determined to be the sub-thread.
The defined class is also defined with a display method, when constructing a system Toast object based on the context object in the constructor of the defined class is completed, the Toast content of the constructed system Toast object is displayed by the display method, and the display method specifically comprises the following steps:
public void show(String msg)
the method is mainly used for displaying the content of the toast, initialization operation on the toast and the like is not needed when the toast is displayed, attention to whether the current thread is a sub-thread or a main thread is not needed, and attention is only needed to the displayed content information, so that the use convenience of displaying the toast is improved, and the development efficiency is further improved.
Since one context object is held in the construction function of DYToast for displaying the Toast content of the main thread, it is possible to construct a systematic Toast object by using the context object and display the Toast content. The Toast object of the system has a static method makeText (Context, charSequence text), the method needs to transmit two parameters for calling, one is a Context object, the Context object is the Context object initialized in the DYTOAST constructor, charSequenceext text represents the content to be displayed, one parameter in the display method defined above is msg parameter of String type, the String is invariable, the String is character String of programming language, msg is a structure, since the String type is subclass of CharSequence, the CharSequence is an interface, represents a readable sequence of char value, the object of String type can be directly assigned to the object of CharSequence type according to polymorphism of object oriented language, thus the msg parameter can be directly transmitted to makeText method. Thus, a system Toast object is constructed, and Toast is displayed on the screen of the Android device by calling a show () method in the system object method.
S4: and the sub-thread sends an empty message to the message loop of the main thread through the tool Handler, and the main thread receives the message and calls the main thread to display the Toast content. Specifically, when the current thread is a sub-thread, the sub-thread sends an empty message to the message loop of the main thread through the tool class Handler, the main thread receives the message, and then a system Toast object is constructed based on the context object in the constructor of the definition class to display Toast content.
A tool type Handler for communication between a sub thread and a main thread is provided in an Android system, and a message can be sent to the main thread in the sub thread through the tool type Handler. Based on the functional characteristic, when a toast needs to be displayed in the child thread, the display of the toast content is realized by the handler.
If the thread is a sub-thread, an empty message is sent to the message loop of the main thread by a postEmptyMessage (SHOW _ TOAST) method in the handler at the moment, the empty message does not carry additional data content, but carries some information in a message structure, so that a SHOW _ TOAST message request is received in the message queue of the main thread, and because the message receiving place is the main thread, the main thread can be directly called to display the TOAST content by the main thread, so that the purpose of displaying the message content in the sub-thread is achieved.
The method for designing Toast comprises the steps of defining a class, defining an object obtaining method in the defined class, defining a context object in a constructor of the defined class, obtaining the defined class by a current thread when Toast display is required, judging whether the current thread is a main thread or a sub thread, and constructing a system Toast object based on the context object in the constructor of the defined class to display Toast content if the current thread is the main thread; if the system is a sub-thread, the sub-thread sends an empty message to the message loop of the main thread through the tool Handler, the main thread receives the message and calls the main thread to display the Toast content, so that the display of the main thread and the sub-thread on the Toast is realized, the compatibility of the system on the Toast display is expanded, the development efficiency is greatly improved, and meanwhile, the convenience of the Toast display of the embodiment of the invention is far higher than that of the Toast display method provided by the system.
In addition, corresponding to the above Toast designing method, the present invention further provides a storage medium, where a computer program is stored on the storage medium, and the computer program, when executed by a processor, implements the steps of the Toast designing method described in the above embodiments. The storage medium includes various media capable of storing program codes, such as a usb disk, a removable hard disk, a ROM (Read-Only Memory), a RAM (Random Access Memory), a magnetic disk, or an optical disk.
Referring to fig. 2, in response to the above Toast designing method, the present invention further provides an electronic device, which includes a memory and a processor, where the memory stores a computer program running on the processor, and the processor executes the computer program to implement the Toast designing method according to the above embodiments.
The embodiment of the invention also provides a Toast design system based on the Toast design method, which comprises a definition module, a judgment module and an execution module.
The definition module is used for defining a class, defining an object acquisition method in the defined class and defining a context object in a constructor of the defined class, wherein the object acquisition method is used for acquiring the defined class by a current thread; the judging module is used for acquiring the defined class by the current thread when the Toast display is required, and then judging whether the current thread is a main thread or a sub thread; the execution module is used for constructing a system Toast object based on the context object in the defining class constructing function to display Toast content if the current thread is a main thread; if the main thread is the sub thread, the sub thread sends an empty message to the message loop of the main thread through the tool Handler, and the main thread receives the message and calls the main thread to display the Toast content.
The method for obtaining the object is a public method, the method for obtaining the object is declared to be a static method, and the context object is at an application level. When the current thread is a sub-thread, the sub-thread sends an empty message to the message loop of the main thread through the tool type Handler, the main thread receives the message, and then a system Toast object is constructed based on the context object in the constructor of the definition class to display Toast content.
The Toast design system of the embodiment of the invention has the advantages that a defining module defines a class, an object obtaining method is defined in the defined class, a context object is defined in a constructor of the defined class, when Toast display is required, the defined class is obtained by a current thread, then a judging module judges whether the current thread is a main thread or a sub-thread, and if the current thread is the main thread, a system Toast object is constructed based on the context object in the constructor of the defined class to display Toast content; if the system is a sub-thread, the sub-thread sends an empty message to the message loop of the main thread through the tool Handler, the main thread receives the message and calls the main thread to display the Toast content, display of the main thread and the sub-thread on the Toast is achieved, compatibility of the system on the Toast display is expanded, and development efficiency is greatly improved.
The present invention is not limited to the above-described embodiments, and it will be apparent to those skilled in the art that various modifications and improvements can be made without departing from the principle of the present invention, and such modifications and improvements are also considered to be within the scope of the present invention. Those not described in detail in this specification are within the skill of the art.

Claims (9)

1. A Toast design method is suitable for an Android platform and is characterized by comprising the following steps:
defining a class, and defining an acquisition object method in the defined class, wherein the acquisition object method is used for acquiring the defined class by the current thread;
defining a context object in the constructor of the defined class, wherein the context object is an application level;
when Toast display is needed, the defined class is acquired by a current thread, then whether the current thread is a main thread or a sub-thread is judged, if yes, a system Toast object is constructed based on a context object in a construction function of the defined class to display Toast content; if it is a child thread, then
The sub-thread sends an empty message to the main thread in a message loop through the tool Handler, and the main thread receives the message and calls the main thread to display the Toast content;
wherein, for the judgment that the current thread is the main thread or the sub-thread, specifically:
defining an abnormal capture code block;
popping a Toast with empty content and transparent background in the abnormal capturing code block;
if the content is empty and the popup of the transparent background Toast is successfully executed, the current thread is a main thread, and if the content is empty and the popup of the transparent background Toast is failed to be executed, the current thread is a sub thread.
2. The Toast designing method as claimed in claim 1, wherein:
the method for obtaining the object is a public method, and the method for obtaining the object is declared to be a static method.
3. The Toast designing method as claimed in claim 1, wherein: when the current thread is a sub-thread, the sub-thread sends an empty message to the message loop of the main thread through the tool type Handler, the main thread receives the message, and then a system Toast object is constructed based on the context object in the constructor of the definition class to display Toast content.
4. The Toast designing method as claimed in claim 1, wherein: and when the construction of a system Toast object is completed based on the context object in the construction function of the defined class, displaying the Toast content of the constructed system Toast object by using the display method.
5. A storage medium having a computer program stored thereon, characterized in that: the computer program, when executed by a processor, implements the method of any one of claims 1 to 4.
6. An electronic device comprising a memory and a processor, the memory having stored thereon a computer program that runs on the processor, characterized in that: the processor, when executing the computer program, implements the method of any of claims 1 to 4.
7. A Toast design system, comprising:
the definition module is used for defining a class, defining an object obtaining method in the defined class and defining a context object in a constructor of the defined class, wherein the context object is at an application level, and the object obtaining method is used for obtaining the defined class by a current thread;
the judging module is used for acquiring the defined class by the current thread when the Toast display is required, and then judging whether the current thread is a main thread or a sub-thread;
the execution module is used for constructing a system Toast object based on the context object in the constructor of the definition class to display Toast content if the current thread is a main thread; if the thread is the sub thread, the sub thread sends an empty message to the message loop of the main thread through the tool Handler, and the main thread receives the message and calls the main thread to display the Toast content;
wherein, for the judgment that the current thread is the main thread or the sub-thread, specifically:
defining an abnormal capture code block;
popping a Toast with empty content and transparent background in the abnormal capturing code block;
if the content is empty and the popup of the transparent background Toast is successfully executed, the current thread is a main thread, and if the content is empty and the popup of the transparent background Toast is failed to be executed, the current thread is a sub thread.
8. A Toast design system as claimed in claim 7, wherein: the method for obtaining the object is a public method, and the method for obtaining the object is declared to be a static method.
9. A Toast designing system as claimed in claim 7, wherein: when the current thread is a sub-thread, the sub-thread sends an empty message to the message loop of the main thread through the tool type Handler, the main thread receives the message, and then a system Toast object is constructed based on the context object in the constructor of the definition class to display Toast content.
CN201810023297.1A 2018-01-10 2018-01-10 Toast design method, storage medium, electronic device and system Active CN110018813B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201810023297.1A CN110018813B (en) 2018-01-10 2018-01-10 Toast design method, storage medium, electronic device and system

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201810023297.1A CN110018813B (en) 2018-01-10 2018-01-10 Toast design method, storage medium, electronic device and system

Publications (2)

Publication Number Publication Date
CN110018813A CN110018813A (en) 2019-07-16
CN110018813B true CN110018813B (en) 2022-12-02

Family

ID=67188101

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201810023297.1A Active CN110018813B (en) 2018-01-10 2018-01-10 Toast design method, storage medium, electronic device and system

Country Status (1)

Country Link
CN (1) CN110018813B (en)

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112199078A (en) * 2020-11-10 2021-01-08 平安普惠企业管理有限公司 Toast message pushing method and device based on android fragment component and computer equipment

Family Cites Families (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7137099B2 (en) * 2003-10-24 2006-11-14 Microsoft Corporation System and method for extending application preferences classes
CN104572110A (en) * 2015-01-19 2015-04-29 山东大学 Optimizing method of Toast activeX in android system
US10116601B2 (en) * 2015-02-06 2018-10-30 Jamdeo Canada Ltd. Methods and devices for display device notifications
CN107517188A (en) * 2016-06-16 2017-12-26 大唐软件技术股份有限公司 A kind of data processing method and device based on Android system
CN106383712B (en) * 2016-09-21 2020-03-17 武汉斗鱼网络科技有限公司 Toast component implementation method and system
CN106569798A (en) * 2016-10-13 2017-04-19 上海斐讯数据通信技术有限公司 Android system Toast information control method and system
CN107168688A (en) * 2017-03-23 2017-09-15 武汉斗鱼网络科技有限公司 Toast display methods and device

Also Published As

Publication number Publication date
CN110018813A (en) 2019-07-16

Similar Documents

Publication Publication Date Title
CN107704282B (en) Loading method and device applied to embedded system
CN111596909A (en) Method and medium for visually editing tree structure game logic
CN112765023A (en) Test case generation method and device
CN107122176B (en) Graph drawing method and device
US20180059978A1 (en) Virtual disk expansion method and apparatus
CN106155746B (en) A kind of installation file processing method and processing device, server
CN109101247A (en) The method, apparatus and server of driver are installed
CN104049974A (en) Dynamic control assembly method and system
US8549493B2 (en) Defining a MIDlet region space
CN110018813B (en) Toast design method, storage medium, electronic device and system
CN115291946A (en) Hongmong system transplanting method, device, electronic equipment and readable medium
CN112882690B (en) Application development method and device
TW201108117A (en) Modifying a video resolution of video data in a pre-operating system environment
CN113742224A (en) Test system, method, device, computer equipment and storage medium
CN113296758A (en) Front-end component library construction method and device and storage medium
US10693780B2 (en) Creating endpoints
CN114092590B (en) Electronic device and evaluation method and medium for image rendering performance of electronic device
US7007273B2 (en) Object oriented model of preloading software
CN111381978A (en) Method for accessing application program, storage medium and smart television
CN110837500A (en) Data screening method and device based on local embedded window and computer equipment
CN106021004A (en) User behavior simulation unit, test system and test method
CN107506474B (en) Database operation method and device based on object mapping
CN111399836B (en) Method and device for modifying page attribute
CN109828752B (en) Project code automatic generation method, device, computer equipment and storage medium
CN116302910A (en) Use case retry method, device, equipment and storage medium

Legal Events

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