Conceptual differences between synchronous and asynchronous:
Synchronization, you must perform a problem before you can continue to perform other.
Async, I'll go ahead and execute the other questions, and you can return to me after you're done.
Why do you refer to asynchronous tasks in Android?
Android Boot, will start a thread also known as the main threads, UI thread, but we can not put all the time-consuming tasks to the main thread to complete, this will affect the user experience, that is, we have to open up new threads to perform our task,
However, in Android, (Loading pictures, network programming is a time-consuming operation), after the ANDROID4.0 rules prohibit network operation in the main thread, so that these time-consuming tasks to the child threads,
Problem, there is no UI action in the child thread, and only the UI line friend as the main thread can do the UI, so how do we tell the message back to the user?
Mode 1. We can write a new operation in handler by handler, call SendMessage in the child thread to notify our handler and the new UI
Mode 2. Use Activity.runonuithread (Runnable) To switch the child thread to the UI thread.
Why use Asynctask?
Very simple, people are very lazy, using the above two ways to operate the trouble, so the big boys encapsulated a method of asynchronous execution, so that we directly deal with time-consuming logic, update the UI.
We generally use it to complete network operation, image loading, data transmission and other operations. (We will, of course, choose our third-party framework to complete)
Basic use of Asynctask:
First, it's an abstract class, which means we're going to rewrite his method and then in new
Asynctask<params,progress,result>
Parames: Parameters that are executed when the task is started, such as the incoming URL
Progress: Percentage unit of background execution progress
Result: Results returned after performing an asynchronous operation
(In contrast to Handler, his backstage is a thread pool, and then the data is huge when the back of the show advantage)
Several important methods of Asynctask
@Override protected void OnPreExecute () { super. OnPreExecute (); }
A method that is called before a time-consuming operation is equivalent to the method of our initialization
Note that this method is run in the UI thread
@Override protected string Doinbackground (String ... params) { return null; }
The party is responsible for executing our time-consuming business logic operations, which means that he is running on a child thread, and what to do if you want to update the UI
Call our Publicprogress method to update our UI, This method is run in a child thread
Publishprogress (Object o);
This method indicates updates to our progress, which can be used to update the download progress bar, and so on.
@Override protectedvoid onprogressupdate (Integer ... values) { Super . Onprogressupdate (values); }
This method is a method that executes in the main thread, and once we call publicprogress in the time-consuming logic code, we want to update the progress bar.
This method is called to the UI thread and to the new our progress bar.
Note: The task instance must create Task.excute (initial parameters such as URLs) in the UI thread;
We don't need to call the methods in the task manually
Task can only be executed once
Android-ansynctask Asynchronous tasks