3.7 AsyncTask Asynchronous Tasks
Category Android Basic Tutorial
Introduction to This Section:
This section introduces to you a lightweight class provided by Android for handling asynchronous tasks: AsyncTask. We usually inherit from AsyncTask, implement asynchronous operations within the class, and then feedback the progress of the asynchronous execution to the UI main thread. Well, some of you may not understand some concepts and feel that it is necessary to explain the concept of multithreading, so let's first explain some conceptual things!
1. Related Concepts
1) What is Multithreading:
>
Answer: First, you need to understand these terms: Application, Process, Thread, Multithreading!
Application(Application): A set of instructions (a set of static code) written in a certain language to complete a specific task.
Process(Process): A running program, an independent unit of system scheduling and resource allocation, the operating system will allocate a segment of memory space for each process, the program is executed in sequence, and goes through the complete process of code loading -> execution -> completion!
Thread(Thread): A smaller unit of execution than a process, each process may have multiple threads, a thread must be placed in a process to be executed! Threads are managed by the program!!! And processes are scheduled by the system!!!!
Multithreading Concept(Multithreading): Execute multiple instructions in parallel, and the CPU's time slices are allocated to each thread according to the scheduling algorithm, which is actually time-sharing execution, but the switching time is very short, and users feel it is simultaneous!
A simple example: You are hanging QQ, and suddenly you want to listen to music. Do you need to close QQ and then start XX player? The answer is no, we just open the player to play music, and QQ is still running, right! This is a simple multithreading ~ In actual development, there are also such examples, such as the application is running, and a new version is found, and you want to update it in the background. At this time, we usually open a background thread to download the new version of the apk, but at this time we can still use other functions in the application! This is an example of the use of multithreading ~
2) The concept of Synchronous and Asynchronous:
>
Answer: Synchronous: When we perform a certain function, the call cannot return before the result is obtained! Simply put, you must wait for the previous thing to be done before you can do the next thing; for example, if you are having sex, to avoid getting pregnant, you must wear a condom first, and then have sex, right? Wear a condom -> then have sex, for example, if you don't have a condom, then the operation of having sex will have to wait until you buy a condom and wear it, and then you can start having sex ~ A vivid example, ♪(^∇^*) Asynchronous: It is the opposite of synchronous. When we perform a certain function, we do not need to get the result immediately, and we can normally do other operations. This function can notify or call back to tell us after it is completed; still the above example of downloading in the background, after executing the download function, we don't need to care about its download process, just notify us when the download is completed ~
3) Why Android needs to introduce asynchronous tasks
>
Answer: Because when the Android program starts, a corresponding main thread (Main Thread) is also started at the same time. This main thread is mainly responsible for handling events related to UI! Sometimes we also call it the UI thread! And in Android App, we must follow the rules of this single-threaded model: Android UI operations are not thread-safe and these operations need to be performed in the UI thread! Suppose we are in a non-UI thread, such as in the main thread, new Thread() to open another thread, and then directly modify the value of the UI control inside it; At this time, the following exception will be thrown: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views In addition, there is another point, if we put all the time-consuming operations in the UI thread, if the UI thread does not respond to the request for more than 5 seconds, then At this time, an ANR (Application Not Responding) exception will be triggered, which means the application is unresponsive~ Finally, there is one more point: After Android 4.0, it is forbidden to perform network operations in the UI thread ~ Otherwise, it will report public class MyActivity extends ActionBarActivity {
private TextView txttitle;
private ProgressBar pgbar;
private Button btnupdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txttitle = (TextView)findViewById(R.id.txttitle);
pgbar = (ProgressBar)findViewById(R.id.pgbar);
btnupdate = (Button)findViewById(R.id.btnupdate);
btnupdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyAsyncTask myTask = new MyAsyncTask(txttitle,pgbar);
myTask.execute(1000);
}
});
}
}
Summary of This Section:
>
Well, at the beginning of this section, we introduced the concepts of applications, processes, threads, multithreading, asynchronous, and synchronous operations; then we explained why Android needs to introduce asynchronous operations, and then introduced the usage of AsyncTask. Of course, as mentioned above, asynchronous operations are more commonly used in network operations, which will be used in the AsyncTask when explaining network operations later, so stay tuned ~ This section ends here, thank you ~
-1.0 Android Basic Tutorial Introduction
-1.0.1 Latest Android Basic Tutorial Catalog for 2015
-1.1 Background and System Architecture Analysis
-1.2 Development Environment Setup
-1.2.1 Developing Android APP with Eclipse + ADT + SDK
-1.2.2 Developing Android APP with Android Studio
-1.3 Solving SDK Update Issues
-1.4 Genymotion Emulator Installation
-1.5.1 Git Tutorial on Basic Operations of Local Repositories
-1.5.2 Git Using GitHub to Set Up a Remote Repository
-1.6 How to Play with 9 (Nine Sister) Pictures
-1.7 Interface Prototype Design
-1.8 Project Related Analysis (Various Files, Resource Access)
-1.9 Android Program Signing and Packaging
-1.11 Decompiling APK to Obtain Code & Resources
-2.1 The Concept of View and ViewGroup
-2.2.1 LinearLayout (Linear Layout)
-2.2.2 RelativeLayout (Relative Layout)
-2.2.3 TableLayout (Table Layout)
-2.2.4 FrameLayout (Frame Layout)
-2.2.5 GridLayout (Grid Layout)
-2.2.6 AbsoluteLayout (Absolute Layout)
-2.3.1 TextView (Text Box) Detailed Explanation
-2.3.2 EditText (Input Box) Detailed Explanation
-2.3.3 Button (Button) and ImageButton (Image Button)
-2.3.5 RadioButton (Radio Button) & Checkbox (Checkbox)
-2.3.6 ToggleButton (Toggle Button) and Switch (Switch)
-2.3.7 ProgressBar (Progress Bar)
-2.3.9 RatingBar (Star Rating Bar)
-2.4.1 ScrollView (Scroll View)
-2.4.2 Date & Time Components (Part 1)
-2.4.3 Date & Time Components (Part 2)
-[2.4
5.2.1 Fragment Example: Implementing Bottom Navigation Bar (Method 1)
5.2.2 Fragment Example: Implementing Bottom Navigation Bar (Method 2)
5.2.3 Fragment Example: Implementing Bottom Navigation Bar (Method 3)
5.2.4 Fragment Example: Bottom Navigation Bar + ViewPager for Page Swiping
5.2.5 Fragment Example: A Simple Implementation of a News (Shopping) App List Fragment
6.2 Data Storage and Access: SharedPreferences for Saving User Preferences
6.3.1 Data Storage and Access: An Introduction to SQLite Database
6.3.2 Data Storage and Access: Another Look at SQLite Database
7.1.1 Learning Android Network Programming and Http Protocol
[
11.0 "2015 Latest Android Basic Tutorial" Concludes with Fireworks~
12.2 DrySister Viewing Girls App (First Edition) – 2. Parsing Backend Data
12.4 DrySister Viewing Girls App (First Edition) – 4. Adding Data Caching (Integrating SQLite)