Read about android os handler handlecallback, The latest news, videos, and discussion topics about android os handler handlecallback from alibabacloud.com
(Self) activity, the service belongs to the main thread, in the main thread can update the UI, such as toast. Other threads cannot be used directly. handler can be used for processing, and handler can be used in activity and service.
There will be issues with UI operations in non-UI threads: can't create handler inside thread that has not called logoff. Prepare
the target of the message to itself (the purpose is to process the message, and the message can find the correct handler), and the message is then included in the queue.ExtractionLooper me = Mylooper ();MessageQueue queue = me.mqueue;While (true) {Message msg = Queue.next ();//might blockif (msg! = null) {if (msg.target = = null) {No Target is a magic identifier for the quit message.Return}msg.target.dispatchMessage (msg);msg.recycle ();}}In the loop
(msg.callback! = null) { handlecallback (msg); } else { if ( Mcallback! = null) { if (Mcallback.handlemessage (msg)) { return; } } Handlemessage (msg); } }From the logic of the code, we need to understand what a few variable methods are all about:1) Msg.callback2) Mcallback3) HandlemessageFirst of all, what is Msg.callback?In the message clas
Message: messages, which contain message IDs, message processing objects, and processed data, are unified by MessageQueue, and eventually handled by handler.
Handler: Processor, responsible for message delivery and processing. When using handler, you need to implement the Handlemessage (Message msg) method to handle a particular message, such as updating the UI.
logoff object andLogoff. Loop () creates a message loop queue;You can create a handler object in a new thread (non-main thread) in either of the following ways:
(1) create a logoff object using the logoff. Prepare () method before handler is created.
Class mythread extends thread {public void run () {log. D (constant. tag, messageformat. format ("thread [{0}] -- run... ", thread. currentthread (). getname
Android Handler source code in-depth analysis, androidhandler
Let's take a look at the source code and look at the various principles of the source code. It's easy to use and know why it is the most awesome.
Handler source code analysis, from the steps used to use edge analysis:
1. Create a Handler object: new
of the main thread. In addition, the handleMessage interface of the Handler will be called in the main thread of the handler to process the message.
This involves thread synchronization. First, refer to the following example to understand the thread model of the Handler object:
1. Create a MyHandler project.
2. Add the following code to MyHandler. java:
Package
Let's take a look at a piece of code:
Thread thread = new Thread () {public void run () {// send a Message to the main Thread in the Child thread Message msg = new Message (); msg. what = 200; msg. obj = param; msg. arg1 = 3; handler. sendMessage (msg) ;};}; Handler handler = new Handler () {public void handleMessage (
, it creates a new message and sets its mcallback to the runnable object, and inserts the message into messagequeue.
4) handler functions include processing messages (executing message content) and forwarding messages, which are mainly processed by handlemessage and dispatchmessage functions;
5) in handler, both send message and post runnable are added to messagequeue.
Queue. enqueuemessage (MSG, uptimemill
operations, the time-consuming operation can be executed in other threads. After the operation is completed, the Message is sent (by using the sendMessges method), and then the handleMessage () is used to update the UI ).
HandlerThread inherits from Thread, so it is essentially a Thread. The difference with a normal Thread is that it has a logoff member variable. This loose is actually the encapsulation of the message queue and the queue processing logic. Simply put, it is the message queue +
A few days ago, I discussed the message mechanism in Android with my colleagues and explored the message sending and receiving processes and the relationship with threads. Although we often use these basic things, our understanding of their internal Principles makes it easier and reasonable for us to structure the system and avoid some low-level errors.
This part is described in four sections:
1. responsibilities and relationships
2. Message Loop
3. t
sub-thread.
The following is an example of handler:
Package com. ac. handlertest; import android. app. activity; import android. OS. bundle; import android. OS. handler; import
Objective:The previous article is about bitmap and memory optimization technology, this article to everyone talk about handler.Handler is an important knowledge of the Android system, which is often asked in Android multithreaded interviews and is often used in real-world projects. Of course, more complex, more knowledge, involved in the class has thread, Looper, Message, MessageQueue.Android is multi-threa
Android applicationsProgramIt is also message-driven. In principle, the message loop mechanism should also be provided. In fact, Google refers to the Message Loop Mechanism of windows and implements the message Loop Mechanism in Android.
Android uses logoff and handler to implement the message loop mechanism.
Hello everyone, this section is about the use of Android handler. before talking about handler, let's raise a small question: how can the program update the title in five seconds. first, let's take a look at the Java programmers who are used to writing programs before they know how to use handler. The Code is as follow
Preface:The previous article is about bitmap and memory optimization technology, this article to everyone talk about handler.Handler is an important knowledge of the Android system, which is often asked in Android multithreaded interviews and is often used in real-world projects. Of course, more complex, more knowledge, involved in the class has thread, Looper, Message, MessageQueue.Android is multi-threade
The handler mechanism is a fairly classic asynchronous messaging mechanism in Android that plays an important role in the history of Android, and is used in many scenarios, whether we are directly facing the application layer or the framework layer. Analysis of the source of a probe. From a common usage, say:Private Button mbtntest;Private
knowledge of Inter-thread synchronization) and sends messages to the asynchronous thread.
4. After the asynchronous thread Handler receives the message, it obtains the Handler of the main thread and sends the message to the main thread.
5. The main thread receives a message from the asynchronous thread.
Note:
1. the main thread Handler or asynchronous thread
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.