) {... mlooper = Looper.mylooper (); if (Mlooper = = null ) {throw new runtimeexception ( "Can ' t create handler inside thread that have not called looper.prepare ()" ); } mqueue = Mlooper.mqueue; Mcallback = callback; masynchronous = async; }The original is directly with Looper assignment, that will not be the processing of this message queue in Lopper it?
handleCallback(Message message) { message.callback.run(); }
Do you feel suddenly enlightened here? When analyzing the Message source code, we know that callback is an instance of the Runnable interface. That is to say, if the Message is not processed, it will be returned to the Message queue. So how does Handler process messages?
public void handleMessage(Message msg) { }
It turned out to be an empty method ...... But it is also norm
--- creates an AsyncTask object. This Bug is extremely concealed and hard to be found.How to restrict caller threads
Under normal circumstances, a Java application process has a thread, and the entry is the main method. Android applications are essentially Java applications, and their main entry is in ActivityThread. main (), The logoff will be called in the main () method. prepareMainLooper (), which initializes the Looper of the main thread and sto
In the process of developing Android, we put time-consuming tasks into sub-threading and refreshing during the process of processing time-consuming tasks and UI interactions. Here are two questions I believe most developers will encounter:1. The data often needs to be read and updated, and it is time consuming to refresh the UI in steps.2. After switching the UI interface, how to stop the data that is being read in the child thread without flushing the old data to the new UI interface.Currently
Handler1. MessageMesssge is a message passed between threads, which can carry a small amount of information inside, to exchange data between different threading, the What field of the message, and in addition to using the Arg1 and Arg2 fields to carry some integral data, using the Obj field to carry a single Object object.2.HandlerHandler the processor, it is used primarily to send and process messages. Sending a message typically uses the handler Sen
.protected voidonCreate (Bundle savedinstancestate) {5.//TODO auto-generated Method Stub6.Super. OnCreate (savedinstancestate); 7. Setcontentview (R.layout.main); 8.//Prints the ID of the current thread9. System.out.println ("activity-->" +Thread.CurrentThread (). GetId ()); 10.//generate a Handlerthread objectHandlerthread Handlerthread =NewHandlerthread ("Handler_thread"); 12.//before using the Handlerthread Getlooper () method, you must first call the class's start () and open a new thread;
. sendToTarget (); Message msg = Message. obtain (); handler. sendMessage (msg); or manually bind it to use msg. setTarget (handler );
HandlerThread automatically contains logoff and does not need to be manually created. It is easy to use.
Because the main thread contains a logoff by default, you do not need to input a logoff. To use Handler in a child thread,
Recently found some information about the communication between Android threads, organized and learned a bit, and made a simple example.Andriod provides Handler and Looper to meet the communication between threads. For example, a sub-thread downloads a picture from the network and sends a message to the main thread when it is downloaded, and this message is passed by binding to the
In an Android application, many time-consuming tasks cannot be placed on the main thread, which is the UI thread, because it can cause a thread to clog and make the system judge that the program is running abnormally, so it requires multiple threads for asynchronous operations, and there are two ways to implement multithreading in Android, which is noted here As long as the UI update is involved, it must be done in the main thread1.HandlerHandler has two important constructs, one is an empty par
New Handler (). Obtainmessage (). Sendtotarget () This sentence is very good, a line of code will be able to handle the asynchronous message! So it's very often used in code, what kind of process is that? What is the mystery of this process? In this article, let's take a step-by-step analysis of these three words.1. Analysis of New Handler ()New Handler () will b
obtain the Looper object of the main thread of the current process.
In android, the UI thread is the main thread of the message, which is created in ActivityThread. java:Public static final void main (String [] args ){Lorule. preparemainlorule ();Logoff. loop (); // message loop Processing}
-->Public static final void preparemainlogoff (){Prepare ();SetMainLooper (myLooper ());If (Process. supportsProcesses ()){MyLooper (). mQueue. mQuitAllowed = fal
1 PackageCom.example.gp08_day26_handler3;2 3 ImportAndroid.os.Bundle;4 ImportAndroid.os.Handler;5 ImportAndroid.os.Looper;6 ImportAndroid.os.Message;7 Importandroid.app.Activity;8 ImportAndroid.view.Menu;9 ImportAndroid.view.View;Ten One /** A * The main thread sends messages to child threads - * @authorQQ - * the */ - Public classMainactivityextendsActivity { - - PrivateHandler Handler;//you need to instantiate in a child thread + @Overrid
Instantiation of the handlerIn Android development, handler is often used to commit a task to a specified thread (such as the main thread) to execute or to delay execution. The handler constructor has several overloaded forms, but eventually it is called to one of the following two types: PublicHandler (Callback Callback,Booleanasync) {Mlooper=Looper.mylooper (); if(Mlooper = =NULL) { Throw NewRuntim
over again.
Three, handler memory leakage analysis and solution1. IntroductionFirst, please browse the following handler code:
public class Sampleactivity extends activity {
private final Handler Mleakyhandler = new Handler () {
@Override
public void Handlemessage (Message msg) {
//...}}
}
Handler is a class that is used to manipulate message queues within a thread. It's a bit round, it's okay, let's take it slow. The previous looper mentioned that Looper is used to create message queues for threads, that is, looper can allow Message Queuing (MessageQueue) to be attached to a thread, and let message queu
object is usually used to interact with logoff. Handler can be seen as an interface of logoff to send messages to a specified logoff and define the processing method.
By default, handler is bound to the loose of the thread in which it is defined. For example, if handler is defined in the main thread, it is bound to the loose of the main thread.
Mainhandler = new
Summary of HandlerWe created the service. Activity,broadcast is a primary thread processing, that is, the UI thread,However, for time-consuming operations, such as large files for I/O reading and writing, database operations and network downloads take a long time, in order not to block the user interface, the ANR Response Prompt window, we can consider using thread threads to solve1. For a thread to refresh a view-based interface, you can use the Postinvalidate () method to process in-thread, wh
common basic data types.
Handler Internal Realization principle
Handler implementation Mechanism:
1,message object that represents a message to be delivered, using a linked list data structure to implement a message pool for reuse,Avoid creating a large number of message objects, resulting in wasted memory2,messagequeue object that holds message queues for message objects, advanced first out principle
MessageQueue Message Queuing, where messages are stored, executed according to FIFO rules, each thread can only have one MessageQueue, in the creation Looper Object creates a MessageQueue object. and MessageQueue The will have a corresponding handler,handler that will send the message to MessageQueue in two ways. ①sendmessage. A Message object is sent thr
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.