comparison of Asynctask and handler
1 The principle of asynctask implementation, and the applicable advantages and disadvantages
Asynctask, a lightweight asynchronous class provided by Android, can directly inherit Asynctask, implement asynchronous operations in a class, and provide the degree to which interface feedback is currently performed asynchronously (UI progress updates can be implemented via interfaces). The final feedback executes the result to the UI main thread.
Advantages of Use:
L Simple, fast
L Process controllable
Disadvantages of using :
L is complicated by the use of multiple asynchronous operations and the need for UI changes.
2) The principle of handler asynchronous implementation and the advantages and disadvantages of its application
In Handler asynchronous implementation, involving Handler, Looper, Message,thread four objects, asynchronous process is the main thread start thread (child thread) àthread (child thread) Run and generate Message-àlooper get the message and pass it to Handleràhandler to get the messages in Looper and make UI changes.
Advantages of Use:
L clear structure, well-defined function
L for multiple background tasks, simple, clear
Disadvantages of using:
L in a single background asynchronous processing, it appears that the code is too much, the structure is too complex (relativity) Asynctask introduced the Android Asynctask than handler more lightweight, suitable for simple asynchronous processing. First of all, the reason Android has handler and Asynctask is to not block the main thread (UI thread), and the UI updates can only be done in the main thread, so asynchronous processing is unavoidable.
Android provides asynctask to reduce the difficulty of this development. Asynctask is a encapsulated background task class, as the name implies is asynchronous task.
Asynctask directly inherits from the object class, and the position is android.os.AsyncTask. To work with Asynctask we provide three generic parameters and overload several methods (at least one at a load).
Asynctask defines three generic types of params,progress and result. Params the input parameters that start the task execution, such as the URL of the HTTP request. Progress the percentage of background task execution. Result background The results returned by the task, such as String.
Students who have used Asynctask know that an asynchronous load of data must at least rewrite the following two methods: Doinbackground (Params ...) in the background, the more time-consuming operations can be placed here. Note that the UI cannot be directly manipulated here. This method is performed in the background thread, and it usually takes a long time to complete the task. Publicprogress can be invoked during execution (Progress ...) To update the progress of the task. OnPostExecute (Result) corresponds to the way handler handles the UI, where you can use the results obtained in Doinbackground to manipulate the UI. This method executes on the main thread, and the result of the task execution is returned as a parameter of this method
If necessary, you will have to rewrite these three methods, but not necessarily: onprogressupdate (Progress ...) You can use the progress bar to increase the user experience degree. This method executes on the main thread to show the progress of the task execution. OnPreExecute () This is the interface that the end user calls Excute, and this method is called before the task executes, and the progress dialog box can be displayed here. Oncancelled () The action to be done when the user calls Cancel
Using the Asynctask class, here are a few guidelines to follow: instances of a task must be created in UI thread; The Execute method must be called in UI thread; Do not manually invoke OnPreExecute (), OnPostExecute ( Result), Doinbackground (Params ...), Onprogressupdate (Progress ...) These methods; The task can only be executed once, otherwise the exception will occur when multiple calls are made;
An example of a super simple understanding of Asynctask:
Main.xml <?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://" Schemas.android.com/apk/res/android " android:orientation=" vertical " android:layout_width= "Fill_parent" android: layout_height= "Fill_parent" > < textview android:id= "@+id/textview01" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" /> <progressbar android:id= "@+id/progressbar02" android : layout_width= "fill_parent" android:layout_height= "Wrap_content"   style= "Android:attr/progressbarstylehorizontal" /> <Button android:id= "@+id/ Button03 " android:layout_width=" fill_parent " android:layout_height= "Wrap_content" android:text= " Update ProgressBar " /> </LinearLayout>
Mainactivity.java package vic.wong.main; import android.app.activity; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.progressbar; import android.widget.textview; Public class MainActivity extends Activity { private Button button; private ProgressBar progressBar; private TextView textView; @Override public void oncreate ( bundle savedinstancestate) { Super.oncreate (savedinstancestate); setcontentview (r.layout.main); button = (Button ) Findviewbyid (r.id.button03); progressbar = (ProgressBar) Findviewbyid (r.id.progressbar02); textView = (TextView) Findviewbyid (r.id.textview01); button.setonclicklistener (new onclicklistener () {