In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you the principle and knowledge of AsyncTask. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
As we all know, UI cannot be updated in child threads, and the asynchronous message processing mechanism must be used. The general practice is to use Handler to send a message to the main thread where necessary, and then process the message to update the UI in the main thread. If many child threads have the need to update the UI, then the scene is a bit difficult to control, and you have to manually handle the Handler message request in the code. The problem will obviously be much more complicated in this way. In fact, android has already taken this situation into consideration for us. AsyncTask was designed by android to solve such problems for us. In fact, if you look at the source code, you will know that the underlying package of AsyncTask is still android's Handler message processing mechanism.
What is AsyncTask
AsyncTask is a set of asynchronous message processing tools encapsulated by android. Using this tool, you can make the processing of asynchronous messages easier, and developers can avoid a lot of trouble. In other words, AsyncTak was designed to do two things:
(1) Update UI in child threads
(2) load data asynchronously
Let's learn the most basic knowledge about it.
First of all, AsyncTask is an abstract class, and its subclasses must be created. But when inheriting the AsyncTask class, you need to specify three generic parameters. The explanations are as follows:
(1) Params
The parameters you need to pass in when performing AsyncTask can be used in background tasks.
(2) Progress
When a background task is executed, if you need to display the current progress on the interface, use the generics specified by Progress as the unit of progress.
(3) Result
When the background task is finished, if you need to return the results, use the generics specified here as the return type.
Then generally in the subclass, you need to rewrite the following callback methods, which are called automatically, not manually in the code.
(1) onPreExexute () this method is called before the background task is executed, that is, the doInBackground method is executed. Some initialization operations are usually performed here, such as displaying a progress bar. (2) doInBackground (Params...) This method is used to perform background tasks, all of its code is operated in child threads, and once the task is completed, it will use return to return the results. Of course, its parameters and the type of return result are the Params and Result we specified above. Be careful not to update the UI in this method. If you need to update the UI element, such as feedback on the progress of the current task, you can call publishProgress (Progress...) Method to do it. (3) onProgressUpdate (Progress...) When the publishProgress method is called in the background task, the method is called automatically, and the parameters in the method are passed by the publishProgress method. This method can update UI. (4) onPostExecute (Result) this method is called when the background task is finished, that is, after the doInBackground is finished. The result returned by doInBackground is the parameters of the method, which allows you to perform the logic after the task is completed, such as closing a progress bar, updating some UI, and so on.
What needs to be explained are these methods. Only the doInBackground method is executed in the child thread, and the rest is executed in the main thread. So what is the way to start and cancel this task? As follows:
MyAsyncTask.execute () where the task yAsyncTask.cancel () cancels the task
All right, after learning the basics above, let's do a little exercise to deepen our impression.
2. A practical exercise
Let's write an AsynTask subclass and actually run it. Create a new project, and then the new subclass inherits from AsynTask, as follows:
1 package com.example.asynctasktest; 2 3 import android.os.AsyncTask; 4 import android.util.Log; 5 6 7 8 public class MyAsyncTask extends AsyncTask {9 10 11 12 protected Void doInBackground (Void... Params) {13 Log.d ("Fu Yong Yi->", "doInBackground"); publishProgress (); 14 return null; 15} 16 17 18 protected void onPreExecute () {19 Log.d ("Fu Yong Yi->", "onPreExecute"); 20 super.onPreExecute (); 21} 22 23 24 protected void onPostExecute (Void result) {25 Log.d ("Fu Yong Yi->", "onPostExecute"); 26 super.onPostExecute (result); 27} 28 29 30 protected void onProgressUpdate (Void...) Values) {31 Log.d ("Fu Yongyi->", "onProgressUpdate"); 32 super.onProgressUpdate (values); 33} 34 35 36 37}
We wrote the simplest AsyncTask subclass, with null arguments passed in, and printed a sentence in each method, mainly to make it easier to observe the order in which they were executed.
Then we modify the MainActivity code as follows:
1 package com.example.asynctasktest; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 67 public class MainActivity extends Activity {8 9 10 protected void onCreate (Bundle savedInstanceState) {11 super.onCreate (savedInstanceState); 12 setContentView (R.layout.activity_main); 13 14 MyAsyncTask mTask = new MyAsyncTask (); 15 mTask.execute (); 16} 17 18}
Then run the program and print the result as follows:
It is easy to see the execution order of the methods from the printed results. As follows:
OnPreExecute- > onProgressUpdate (this method is called only if the publishProgress method is called)-> doInBackground- > onPostExecute
The above is what is the principle and knowledge of AsyncTask. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.