This paper mainly explains the use of Asynctask and the application of handler
First, we need to be clear about the next concept, what is the UI thread. As the name suggests, the UI thread is the thread that manages the user interface!
The Android UI thread operation is not safe, and operations that interact directly with the user must be done in the UI thread. This pattern is called single-threaded mode.
In single-threaded mode, we must be careful: do not block UI threads, make sure that UI components are accessed only in the UI thread
When we are going to perform a complex and time-consuming algorithm and ultimately to reflect the results of the calculation to the UI, we will find that we have no way to ensure that the above two requirements, we will certainly think of opening a new thread, so that this complex and time-consuming task to the background to execute, but the execution is completed? We found that we could no longer interact with the UI.
To solve this situation, Android offers us a lot of options.
1, handler and message mechanism: through the display of the throw, capture messages and the UI to interact;
2), Activity.runonuithread (Runnable): Executes immediately if the current thread is a UI thread, otherwise, the thread action in the parameter is placed in the event queue of the UI thread, pending execution.
3), View.post (Runnable): Put the operation into the message queue, if put successfully, the operation will execute in the UI thread and return True, otherwise return false
4), view.postdelayed (Runnable, long) is basically the same as the third, except that a delay time is added.
5), android1.5 later provides us with a tool class to fix this problem asynctask.
Asynctask is an abstract class that defines three generic type Params,progress,result.
Params an input parameter that initiates a task execution, such as the URL of an HTTP request
Progress the percentage of background task execution.
Result background The results of the final return of the task, such as String
Using a program call, what the developer needs to do is implement these methods.
1) Sub-class of Asynctask
2 Implement the following one or several methods defined in Asynctask
OnPreExecute (), which is invoked by the UI thread before performing the actual background operation. You can do some preparation work in this method, such as displaying a progress bar on the interface.
Doinbackground (Params ...) is executed immediately after the OnPreExecute method is executed, which runs in a background thread. This will be the main responsibility for performing the time-consuming background computing work. You can call the Publishprogress method to update the real-time task progress. This method is an abstract method that must be implemented by subclasses.
Onprogressupdate (Progress ...), after the Publishprogress method is invoked, UI thread calls this method to show the progress of the task in the interface, for example, through a progress bar.
OnPostExecute (Result), after Doinbackground execution completes, the OnPostExecute method is invoked by the UI thread, and the results of the background calculation are passed to UI thread through this method.