Copy Code code as follows:
Package Com.yanjun;
Import android.app.Activity;Import Android.os.Bundle;Import Android.os.Handler;Import Android.os.HandlerThread;Import Android.os.Looper;Import Android.os.Message;
public class Handleractivity extends activity {
@Overridepublic void OnCreate (Bundle savedinstancestate) {Super.oncreate (savedinstancestate);Setcontentview (R.layout.main);Implements the ability to use Looper to process message qu
Java Official website Description of the Looper object:
public class Looperextends ObjectClass used to run a message loop for a thread. Threads By default don't have a message loop associated with them; To create one, call prepare () in the thread, the "is" to run the loop, and then loop () to have it process messages until the The loop is stopped.
Most interaction with a message loop is through the Handler class.
This is a typical example of the i
):Sendmessagedelayed (Message,long):SendMessage This action queues the Message object, which contains some information, and handler's Hanlermessage (message) handles the message.Handlermessage (Message) must be overridden by the Handler subclass.Post This action puts the Runnable object into the Messagequeue,messagequeue when you receive these messages, execute them in a certain sort of order.When posting or sending to a hanler, you can have three behaviors: when the MessageQueue is ready to pro
In the simple music player, with the handler, and not too much to study, here to learn the next Android under the asynchronous message processing mechanism. The handler used here is mainly that the UI cannot be updated in the thread, and it needs to be handler. There are several concepts about asynchronous message processing.1. Message: Messages, data units for inter-thread communication. For example, to download a song in the background and download it. To update the UI, you can send a message
Analysis of asynchronous message processing mechanismAsynchronous message processing in Android consists of four parts, message, Handler, MessageQueue, and Looper.1. MessageA message is an information that is passed between threads, and it can carry a small number of messages inside to exchange data between different threading. In the previous example, the What field of message was used, in addition to using the Arg1 and Arg2 fields to carry some shap
The previous article mentions that the UI thread of an Android system is a thread with a message loop (Looper) mechanism, while Android also provides a Handlerthread class that encapsulates a message loop (Looper) that can bind handler () objects, A message is sent to the thread through the SendMessage () function of handler, and the message received by the thread is processed through the handlemessage () f
if (looper.mylooper () = Looper.getmainlooper ()) {If we finish marking off of the main thread, we need toActually do it in the main thread to ensure correct ordering.Handler mainthread = new Handler (Looper.getmainlooper ());Mainthread.post (New Runnable () {@Overridepublic void Run () {Meventlog.add (tag, threadId);Meventlog.finish (This.tostring ());}});Return}Handler inside holds a looper,looper inside
Relationship between message, Handler, message Queue, Looper in a single-threaded model1. MessageA message is an information that can be understood as a communication between threads. Processing a data background thread requires updating the UI, and you can send a message containing some data to the UI thread.2, HandlerHandler is the processor, is the main processor of the message, responsible for sending the message, message content execution process
1. First Looper.prepare () saves an looper instance in this thread, and then holds a MessageQueue object in the instance, because Looper.prepare () can only be called once in a thread. So MessageQueue will only exist in one thread.2. Looper.loop () causes the current thread to enter an infinite loop, which reads the message from the MessageQueue instance and then callbacks the Msg.target.dispatchMessage (msg) method.3, handler the construction method,
To do a simple little demo about Handler,looper,message, a button on the main interface of the code, click Send Message (accumulate) to the thread's Looper loop, and then print it out in Logcat:Package Zhangphil.looper;import Android.os.bundle;import Android.os.handler;import android.os.looper;import Android.os.message;import Android.util.log;import Android.view.view;import Android.widget.button;import Andr
Calls in the thread containing the creation of the handler method, will be an error, prompt:"Need call Looper.prepare ()"--before creating, invoke the Looper.prepare () method to create a LooperHowever, the method that contains the create handler may be called in the main thread or in a child thread.When called in the main thread, you add the Looper.prepare () method to it, it may error, prompt:"Only one Looper can be created per thread"--within each
ThreadLocalBefore looking down, you need to know about Java's threadlocal class, you can refer to the blog:Decryption threadlocalLooper, Handler and MessageQueueLet's analyze the previous code and see what the relationship between Handle, Looper, and MessageQueue is in the handler mechanism. Packagecn.lixyz.handlertest;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Looper;ImportAndroid.os.Message;ImportAnd
This class of users runs the message loop in a thread. The thread does not have a message loop by default, and you can call prepare () to create a run loop in the threads, and then call Loop () to process the message until the loop endsMost of the message loop interactions are through the handler class;Class Looperthread extends thread{Public Handler handler;//Create Handler object in threadpublic void Run () {Looper.prepare ();//The thread must be created atHandler=new handler () {public void H
1. A handler has only one queue;2. When invoking the Handler.post (Runnable Runnable) method, the Runnable is encapsulated as a message;3. When the queue executes, it determines whether the current message encapsulates the runnable, and if it is
Android messaging SystemOverall frameworkIn the Android messaging system, each thread has a looper,looper in which a Messagequeue,handler delivers a message to the queue Message,looper loops out of the message to be processed by handler. The whole
ι Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced. Looper is used for message loops in the Android messaging Mechanism. It will keep looping, go to the MessageQueue to see if there is a new
This article is mainly to handler and message loop implementation principle of source analysis, if not familiar with handler can see the blog "android handler use", which is why Android introduced handler mechanism and how to use handler to
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.