A few days ago interview a training course, the teacher asked the two Android questions, the results are not answered, (~ ̄ (OO)  ̄) ブ
One is about the ListView, I have not used it very much, no. One is about Asynctask, never heard of, more useless ... (⊙o⊙) True
One of the meeting didn't ask. ~ (≧▽≦)/~
Come and learn Asynctask now.
First, introduce Asynctask:
When developing an Android mobile client, you tend to use multithreading to do it, and we typically put time-consuming operations on separate threads to avoid taking the main thread and creating a bad user experience for the user. However, the main thread (UI thread) cannot be manipulated in a child thread, and an error occurs when manipulating the UI thread in a child thread. So Android provides a class handler to update the UI thread in a child thread, updating the UI interface with a message-sending mechanism and presenting it to the user. This solves the problem of the child thread updating the UI. However, time-consuming task operations always start some anonymous sub-threads, and too many sub-threads bring a huge burden on the system, resulting in some performance problems. So Android provides a tool class Asynctask, which, as the name implies, executes tasks asynchronously. This asynctask is born to handle some of the more time-consuming tasks behind the scenes, to give users a good user experience, from the programming syntax is much more elegant, no longer need child threads and handler to complete the asynchronous operation and refresh the user interface.
Ii. constructing the parameters of the Asynctask sub-class
Asynctask<params,progress,result> is an abstract class
Typically used for inheritance, inheriting asynctask needs to specify the following three generic parameters:
Params: Enter the type of the parameter when starting the task, such as the URL of the HTTP request.
Progress: Type of progress value returned in background task execution, percentage of background task execution
Result: The type of the result returned when the background execution task is completed, such as String,integer.
Third, construct callback method of Asynctask subclass
1..execute (params ... params), performing an asynchronous task that requires us to call this method in code to trigger the execution of an asynchronous task.
2.onPreExecute: Before the background time-consuming operation is called, usually the user completes some initialization operation, you can display the progress dialog box here.
3, Dolnbackground: must rewrite, asynchronously execute the task that the background thread will complete
4, OnPostExecute (), when Dolnbackground () is complete, the system automatically calls the OnPostExecute () method,
and pass the return value of the Dolnbackground () method to the method, showing the result after processing
5. Onprogressupdata: Call the Pubilshprogress () method in the Dolnbackground () method
The method is triggered back when the task's execution progress is updated. Used to show the progress of task execution.
6, oncancelled (), called when the user cancels the thread operation. Called when oncancelled () is called in the main thread.
There are several methods that are regular.
(1) First execute the OnPreExecute () method
(2) Implement the Dolnbackground () method
(3) Implement the OnPostExecute () method
The Pubilshprogress () method is performed manually in Dolnbackground () to update the progress of the task.
Iv. several guidelines that must be followed:
In use, there are a few things that require extra attention:
1. An instance of an asynchronous task must be created in the UI thread.
The 2.execute (params ... params) method must be called in the UI thread.
3. Do not manually call OnPreExecute (), doinbackground (params ... params), onprogressupdate (Progress ... values), OnPostExecute ( Result result) of these several methods.
4. You cannot change the UI component information in the Doinbackground (params. params).
5. A task instance can only be executed once, and if executed the second time will throw an exception
V. Simple example
Not to Be continued ~ ~ ~
Android Development _ talking about Asynctask