1. To solve the problem that the UI component cannot be updated by a new thread, Android provides the following solutions.
1) Use handler to implement communication between threads;
2) activity. runonuithread (runnable );
3) view. Post (runnable );
4) view. postdelayed (runnable, long ).
Ii. asynctask is suitable for simple asynchronous processing and can be implemented without using threads and handler. Asynctask <Params, progress, result> is an abstract class, which is generally used for inheritance. When inheriting asynctask, you must specify the following three generic parameters.
1) Params: input parameter type for task startup
2) progress: the type of progress value for background task completion
3) result: the type of result returned after the background task execution is completed
Iii. steps for implementing asynctask:
1) create a subclass of asynctask and specify the type for the three generic parameters. If you do not need to specify a type for a generic parameter, you can specify it as void.
2) Implement asynctask as needed:
(1) doinbackground (Params...): rewrite this method to be the task to be completed by the background thread. You can call publishprogress (Progress... values) to update the task execution progress.
(2) onprogressupdate (Progress .. values): This method is triggered when the publishprogress () method is called in the doinbackground () method to update the task execution progress.
(3) onpreexecute (): This method will be called before time-consuming operations in the background are executed. This method is usually used to complete initialization preparation, such as displaying a progress bar on the interface.
(4) onpostexecute (result): After doinbackground () is completed, the system automatically calls the 0 npostexecute () method and passes the return value of the doinbackground () method to this method.
3) Call execute (Params... Params) of the asynctask subclass to start time-consuming tasks.
4. When using asynctask, follow the following rules:
1) You must create an asynctask instance in the UI thread.
2) The execute () method of asynctask must be called in the UI thread.
3) asynctask onpreexecute (), onpostexecute (result), doinbackground (Params ...), onprogressupdate (Progress .. values) method, not called by the programmer code, but by the Android system.
4) Each asynctask can be executed only once, and multiple calls will cause exceptions.
Asynctask (asynchronous task)