When developing Android applications, you must follow the single-thread model principle: Android UI operations are not thread-safe and must be executed in the UI thread. In a single-threaded model, you must always remember the following two rules:
1. Do not block the UI thread
2. Make sure to only access the Android UI toolkit in the UI thread
When a program is started for the first time, Android starts a corresponding main thread at the same time. The main thread is mainly responsible for processing UI-related events, such as user key events, the user contacts screen events and Screen Drawing events, and distributes related events to corresponding components for processing. Therefore, the main thread is often called the UI thread. For example, you can obtain a webpage from the Internet and display its source code in a TextView. This kind of program involving network operations generally requires a thread to complete network access. However, after obtaining the page source code, yes. You cannot directly call TextView in the Network Operation thread. setText () is because other threads cannot directly access the main UI thread member.
Android provides several methods to access the UI thread in other threads.
View plaincopy to clipboardprint?
Activity. runOnUiThread (Runnable)
View. post (Runnable)
View. postDelayed (Runnable, long)
Activity. runOnUiThread (Runnable)
View. post (Runnable)
View. postDelayed (Runnable, long)
Hanlder classes or methods also make your code complex and hard to understand. However, when you need to implement complex operations and frequently update the UI, this will become worse. To solve this problem, Android 1.5 provides a tool class: AsyncTask, which makes it easier to create long-running tasks that need to interact with the user interface. It can be implemented without using threads and Handler.
AsyncTask is an abstract class. AsyncTask defines three generic types: Params, SS, and Result.
Input parameters for Params startup task execution, such as the URL of the HTTP request.
Percentage of Progress background tasks.
Result: The Result returned when the task is executed in the background, such as String.
The execution of AsyncTask is divided into four steps. Each step corresponds to a callback method, which should not be called by the application. All developers need to do is implement these methods.
1) subclass AsyncTask
2) implement one or more of the following methods defined in AsyncTask
OnPreExecute (), this method will be called by the UI thread before the actual background operation is executed. You can make some preparations in this method, such as displaying a progress bar on the interface.
DoInBackground (Params...) will be executed immediately after the onPreExecute method is executed. This method runs in the background thread. Here, we will primarily perform those time-consuming background computing tasks. You can call publishProgress to update the real-time task progress. This method is an abstract method and must be implemented by sub-classes.
OnProgressUpdate (Progress...), after the publishProgress method is called, UI thread will call this method to display the Progress of the task on the interface, for example, display through a Progress bar.
OnPostExecute (Result). After doInBackground is executed, the onPostExecute method will be called by the UI thread, and the background computing Result will be passed to the UI thread through this method.
To correctly use the AsyncTask class, the following guidelines must be followed:
1) The Task instance must be created in the UI thread.
2) The execute method must be called in the UI thread.
3) do not manually call the onPreExecute (), onPostExecute (Result), doInBackground (Params...), onProgressUpdate (Progress...) methods.
4) The task can only be executed once. Otherwise, exceptions may occur during multiple calls.
Author: "Li Xin's Crazy Android life ..."