when it comes to developing applications, our programmers are more concerned with threading.
when an Android app is turned on, a main thread is turned on by default (most of them are UI-related and so called UI threads).The avtivity component, service component, Broadcastreceiver component that runs on top of the UI thread.
when the user operates the main thread, it is important to note that time-consuming operations are not allowed in the UI thread. For example: complex logic calculations, hibernation, networking, downloading data, etc. are time-consuming operations. If you do a time-consuming operation in the main thread of the UI, once the application throws an exception for more than 5 seconds, an application not respond application fails to respond, and exits immediately.
demo of ANR Error:
So where does the time-consuming operation run? This is the time to use threads. this will not produce the above error. As shown below:
Let the child thread do the time-consuming work. Note: Controls in Android can only be manipulated by the UI main thread and cannot be manipulated by other threads, otherwise there will be an exception "
Handlerthe handler is responsible for sending messages in the child thread. the handler is responsible for receiving messages in the main thread. implements communication between the child thread and the main thread. Handler MyHandler = new MyHandler () run in the main thread. When the main thread receives a message, the main thread accepts the message via Handlemessage (msg)
Asynchronous Task Class Asynctask:
[1]handler mode needs to create a new thread for each task, and after the task completes, it sends a message to the UI thread through the handler object, which completes the interface update, which controls the entire interface very finely. But there are drawbacks, such as code bloat, so to simplify operations, the Android 1.5 SDK provides a tool class Asynctask (asynchronous Task Class), making it easier to create asynchronous tasks. [2] The execution of an asynchronous task typically consists of the following steps:[1]execute (params ... params), performing an asynchronous task requires us to call this method in code to trigger the execution of an asynchronous task. [2]onpreexecute (), executed immediately after the Execute (params ... params) is invoked, is typically used to mark the UI before performing a background task. [3]doinbackground (params) params), executed as soon as OnPreExecute () is completed, is used to perform a more time-consuming operation, which accepts input parameters and returns the result of the calculation. You can call Publicprogress (progress...values) during execution to update the progress information. [4]onprogressupdate (Progress ... values), this method is executed when calling publicprogress (Progress ... values) and updates the progress information directly to the UI component. [5]onpostexecute (result result), when the background operation ends, this method is called, the result is passed as a parameter to this method, and the result is displayed directly on the UI component. [3] When using asynchronous tasks, the following points require extra attention:[1] The asynchronous task object must be created in the main thread of the UI. [2] do not manually call Onpreexcute (), doinbackground (params ... params), onprogressupdate (Progress ... values), OnPostExecute ( Result result)[3] The UI control can no longer be manipulated in doinbackground (params ... params).
The asynchronous task class is an encapsulation of the handler, and the principle is the same, creating the class as follows:
related parameters:
What the parameters represent:Params: The type of the parameter entered when the task is started, and if no input parameters are required, enter viod This parameter can only fill in the Reference data type progress: the type of progress that the background task executes.
Result: The type of the background calculation result
For example: Note that the time-consuming operation is put in this method, note: The location of the passed in parameter
Detailed code parsing:
<span style= "color: #660000;" > Package Com.hqyj.asyctask;import android.os.asynctask;import android.widget.textview;//params The input parameter type when the task is started. Note: This parameter can only fill in the reference data type, if there is no parameter input, fill in the Void//progress background task execution of the type of progress. The type of result that results is computed in the background. public class Myasynctask extends Asynctask<integer, Integer, string> {<span style= "white-space:pre;" ></span>textview TV; <span style= "WHITE-SPACE:PRE;" ></span>int count; <span style= "WHITE-SPACE:PRE;" ></span>public Myasynctask (TextView TV) {<span style= "white-space:pre;" ></span>//TODO auto-generated constructor stub <span style= "White-space:pre;" ></span> this.tv = TV; <span style= "WHITE-SPACE:PRE;" ></span>} <span style= "White-space:pre;" ></span>//The method quilt thread calls <span style= "WHITE-SPACE:PRE;" ></span>//put the time-consuming operation in the method <span style= "WHITE-SPACE:PRE;" ></span> @Override <span style= "WHITE-SPACE:PRE;" ></span>protected String Doinbackground (INteger. Params) {<span style= "white-space:pre;" ></span>/TODO auto-generated method stub <span style= "White-space:pre;" ></span> while (Count < Params[0]) {<span style= "white-space:pre;" ></span>//requires the system to call the Onprogressupdate method. <span style= "WHITE-SPACE:PRE;" ></span> try {<span style= "white-space:pre;" ></span> Thread.Sleep (1000); <span style= "WHITE-SPACE:PRE;" ></span> catch (Interruptedexception e) {<span style= "white-space:pre;" ></span>/TODO auto-generated catch block <span style= "WHITE-SPACE:PRE;" ></span> E.printstacktrace (); <span style= "WHITE-SPACE:PRE;" ></span>} <span style= "White-space:pre;" ></span> count++; <span style= "WHITE-SPACE:PRE;" ></span> this.publishprogress (count); <span style= "WHITE-SPACE:PRE;" ></span>} <span style= "White-space:pre;" ></span> return "task End"; <span style= "White-sPace:pre; " ></span>}<span style= "WHITE-SPACE:PRE;" ></span>//This method is called by the UI main thread <span style= "WHITE-SPACE:PRE;" ></span>//<span style= "WHITE-SPACE:PRE;" ></span> @Override <span style= "WHITE-SPACE:PRE;" ></span>protected void Onprogressupdate (Integer ... values) {<span style= "white-space:pre;" ></span>/TODO auto-generated method stub <span style= "White-space:pre;" ></span> super.onprogressupdate (values); <span style= "WHITE-SPACE:PRE;" ></span> Tv.settext (string.valueof (values[0)); <span style= "WHITE-SPACE:PRE;" ></span>} <span style= "White-space:pre;" ></span>//Asynchronous task starts with <span style= "WHITE-SPACE:PRE;" ></span> @Override <span style= "WHITE-SPACE:PRE;" ></span>protected void OnPreExecute () {<span style= "white-space:pre;" ></span>/TODO auto-generated method stub <span style= "White-space:pre;" ></span> Super.onpreexecute (); &lT;span style= "WHITE-SPACE:PRE;" ></span> tv.settext ("task Start"); <span style= "WHITE-SPACE:PRE;" ></span>} <span style= "White-space:pre;" ></span>//called <span style= "White-space:pre when the asynchronous task ends." ></span> @Override <span style= "WHITE-SPACE:PRE;" ></span> protected void OnPostExecute (String result) {<span style= "white-space:pre;" ></span>/TODO auto-generated method stub <span style= "White-space:pre;" ></span> Super.onpostexecute (Result); <span style= "WHITE-SPACE:PRE;" ></span>tv.settext (result);} </span>
Application of Android thread handler and Asynctask