Comparison between asynctask and handler
1) asynctask implementation principle, and applicable advantages and disadvantages
Asynctask is a lightweight asynchronous class provided by Android. It can inherit asynctask directly and implement asynchronous operations in the class,AndProvide interface feedback to the currentAsynchronous execution degree(Ui progress update can be implemented through the interface), and finally the execution result is fed back to the main UI thread.
Advantages:
L simple and fast
L process controllable
Disadvantages:
L it becomes complicated when multiple asynchronous operations and UI changes are required.
2) principle and advantages and disadvantages of handler asynchronous implementation
When handler is implemented asynchronously, it involves four objects: handler, logoff, message, and thread. the asynchronous process is that the main thread starts thread (subthread) à thread (subthread) run and generate message-à logoff to get the message and pass it to handler. the handler gets the message in logoff one by one and changes the UI.
Advantages:
L clear structure and clear Function Definition
L simple and clear for multiple background tasks
Disadvantages:
L when asynchronous processing is performed on a single backendCodeToo many, too complex structure (relative)
Introduction to asynctask Android asynctask is more lightweight than handler and is suitable for simple asynchronous processing. First, it is clear that android has handler and asynctask to avoid blocking the main thread (ui thread), and UI Updates can only be completed in the main thread, so asynchronous processing is inevitable.
Android provides asynctask to reduce the development difficulty. Asynctask is a encapsulated background task class. as its name implies, it is an asynchronous task.
Asynctask directly inherits from the object class and is located in Android. OS. asynctask. To use asynctask, we need to provide three generic parameters and reload several methods (at least one ).
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.
All the students who have used asynctask know that the following two methods are required to load data asynchronously:
- Doinbackground (Params ...) The operations in the background can be put here. Note that you cannot directly operate the UI here. This method is executed in the background thread to complete the main work of the task, usually takes a long time. You can call publicprogress (progress…) during execution ...) To update the task progress.
- Onpostexecute (result) is equivalent to handler's way of processing the UI, where you can use the result processing operation UI obtained in doinbackground. This method is executed in the main thread. The task execution result is returned as a parameter of this method.
If necessary, you have to rewrite the following three methods, but they are not required:
- Onprogressupdate (progress ...) You can use the progress bar to increase user experience. This method is executed in the main thread to display the task execution progress.
- Onpreexecute () is the interface used by the end user to call excute. This method is called before the task is executed. The progress dialog box is displayed here.
- Oncancelled () the operation to be performed when the call is canceled
When using the asynctask class, the following guidelines must be followed:
- Task instances must be created in the UI thread;
- The execute method must be called in the UI thread;
- Do not manually call the onpreexecute (), onpostexecute (result), doinbackground (Params...), onprogressupdate (Progress...) methods;
- This task can only be executed once. Otherwise, exceptions may occur during multiple calls;
An example of a simple understanding of asynctask:
Main. xml
[Java] View plaincopy
- <? 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
[HTML] View plaincopy
-
- 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;
-
- Private progressbar;
- Private 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 (){
-
-
- @ Override
- Public void onclick (view v ){
-
- ProgressbarasynctaskAsynctask=NewProgressbarasynctask (textview, progressbar );
-
- Asynctask.exe cute (1000 );
-
- }
-
- });
- }
-
- }
Netoperator. Java
[HTML] View plaincopy
-
- Package Vic. Wong. Main;
-
-
-
- // Simulate the Network Environment
- Public class netoperator {
-
-
- Public void operator (){
-
- Try {
-
- // Sleep for 1 second
-
- Thread. Sleep (1000 );
-
- } Catch (interruptedexception e ){
- // Todo auto-generated Catch Block
-
- E. printstacktrace ();
-
- }
-
- }
-
-
- }
Progressbarasynctask. Java
[HTML] View plaincopy
-
- Package Vic. Wong. Main;
-
- Import Android. OS. asynctask;
-
- Import Android. widget. progressbar;
- Import Android. widget. textview;
-
-
- /**
-
- * After the class object is generated and the execute method is called
-
- * The onproexecute method is executed first.
-
- * Second, execute the doinbackgroup method.
-
- *
-
- */
-
- Public class progressbarasynctask extends asynctask<Integer, Integer, string>{
-
- Private textview;
-
- Private progressbar;
-
-
-
- Public progressbarasynctask (textview, progressbar ){
-
- Super ();
-
- This. textview= Textview;
- This. progressbar= Progressbar;
-
- }
-
-
-
- /**
-
- * The integer parameter corresponds to the first parameter in asynctask.
-
- * The string return value corresponds to the third parameter of asynctask.
-
- * This method is not run in the UI thread and is mainly used for asynchronous operations. All spaces in the UI cannot be set or modified in this method.
-
- * However, you can call publishprogress to trigger onprogressupdate to operate the UI.
- */
-
- @ Override
-
- Protected string doinbackground (integer... Params ){
-
- NetoperatorNetoperator=NewNetoperator ();
-
- IntI=0;
- For (I=10; I<= 100; I + = 10 ){
-
- Netoperator. Operator ();
-
- Publishprogress (I );
-
- }
-
- Return I + Params [0]. intvalue () + "";
-
- }
-
-
- /**
-
- * The string parameter corresponds to the third parameter in asynctask (that is, receiving the return value of doinbackground)
-
- * After the doinbackground method is executed, it runs in the UI thread. You can set the UI space.
-
- */
-
- @ Override
-
- Protected void onpostexecute (string result ){
-
- Textview. settext ("Asynchronous Operation execution ends" + result );
-
- }
-
-
- // This method runs in the UI thread and can be set in the UI thread.
-
- @ Override
-
- Protected void onpreexecute (){
-
- Textview. settext ("Starting asynchronous thread execution ");
-
- }
-
-
-
- /**
-
- * The intege parameter corresponds to the second parameter in asynctask.
- * In the doinbackground method, each call to the publishprogress method triggers the onprogressupdate execution.
-
- * Onprogressupdate is executed in the UI thread. All operations can be performed on the UI space.
-
- */
-
- @ Override
-
- Protected void onprogressupdate (integer... values ){
-
- IntVlaue=Values[0];
-
- Progressbar. setprogress (vlaue );
- }
-
-
-
-
-
-
- }