1. Message
A 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 section we used the What field of the message, in addition to using the Arg1 and Arg2 fields to carry some integral data, using the Obj field to carry an object.
2. Handler
Handler, as the name implies, is the meaning of the processor, which is primarily used to send and process messages. Sending a message is typically using the handler SendMessage () method, and the emitted message is eventually passed to the handler Handlemessage () method after a series of tossing and processing.
3. MessageQueue
MessageQueue is the meaning of Message Queuing, which is primarily used to store all messages sent through handler. This part of the message will remain in the message queue and wait for it to be processed. There will only be one MessageQueue object in each thread.
4. Looper
Looper is the steward of MessageQueue in each thread, and after calling the Looper loop () method, it enters an infinite loop, and then whenever a message is found in MessageQueue, it is taken out and passed to handler In the Handlemessage () method. There will only be one Looper object in each thread.
Asynctask: The implementation principle is also based on the asynchronous message processing mechanism, but Android helped us to do a good package.
Asynctask is an abstract class that specifies three generic parameters for the Asynctask class when inheriting, and the purpose of these three parameters is as follows.
1. Params: Parameters that need to be passed in when executing asynctask can be used in a background task.
2. Progress: When the background task executes, if you need to display the current progress on the interface, use the generic type specified here as the progress unit.
3. Result: When the task finishes executing, if the result needs to be returned, the generic type specified here is used as the return value type.
One of the simplest custom asynctask can be written in the following ways:
Class Downloadtask extends Asynctask<void, Integer, boolean> {
......
}
You need to rewrite several methods in Asynctask to complete the customization of the task. There are four ways to rewrite frequently.
1. OnPreExecute ()
This method is called before the background task begins execution, and is used for initialization of some interfaces, such as displaying a progress bar dialog box.
2. Doinbackground (Params ...)
All the code in this method will run in a sub-thread, and we should be here to handle all the time-consuming tasks. Once the task is completed, the result of the task execution can be returned by the return statement, and if Asynctask's third generic parameter specifies void, the task execution result is not returned. Note that the UI action is not allowed in this method, and if you need to update the UI elements, such as feedback on the progress of the current task, you can call Publishprogress (Progress ...). method to complete.
3. Onprogressupdate (Progress ...)
When Publishprogress (Progress ...) is called in a background task Method, the method is called quickly, and the parameters that are carried in the method are passed in the background task. The UI can be manipulated in this method, and the interface elements can be updated accordingly using the values in the parameters.
4. OnPostExecute (Result)
This method is called quickly when the background task is completed and returned through a return statement. The returned data is passed as a parameter to this method, and the returned data can be used to perform some UI actions, such as reminding the result of the task execution, and closing the Progress Bar dialog box.
Android Note: Asynchronous Message Processing