Asynchronous operations in Android are suitable for Data Interaction Between Android clients and servers based on the network and user experience.
1. Compile an asynchronous operation class to inherit asynctask and implement the corresponding method;
2. Compile an activity Interface Class firstasyncactivity and call the execute method to execute the corresponding methods in the asynchronous operation class;
3. When executing the asynchronous operation class, the onpreexecute method is first executed, which runs in the UI thread;
4. the second step is to execute the doinbackground method, which is not running in the UI thread, but to open a thread separately. In this method, you can call the publicprogress method to execute the onprogressupdate method, the latter is running in the UI thread;
5. The onpostexecute method is last executed, which runs in the UI thread.
Note: asynctask <integer, integer, string>
Integer indicates the parameter type in the doinbackground method;
Integer indicates the parameter type in the onprogressupdate method;
String indicates the return value type in the doinbackground method.
The following code is used:
Package com. duoguo. Android;
Import com. duoguo. Android. async. firstasynctask;
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;
/**
* @ Author Samsung
*
*/
Public class firstasyncactivity extends activity {
Private textview;
Private progressbar;
Private button startbutton;
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. async );
Textview = (textview) findviewbyid (R. Id. TV _tip );
Progressbar = (progressbar) findviewbyid (R. Id. pb_progress );
Startbutton = (button) findviewbyid (R. Id. btn_start );
Startbutton. setonclicklistener (New onclicklistener (){
Public void onclick (view v ){
Firstasynctask = new firstasynctask (textview,
Progressbar );
Firstasynctask.exe cute (100 );
}
});
}
}
Package com. duoguo. Android. async;
Import com. duoguo. Android. threadoperate;
Import Android. OS. asynctask;
Import Android. widget. progressbar;
Import Android. widget. textview;
/**
* Asynchronous implementation Class 1: generate the Class Object and call the execute method;
* 2. Execute the onpreexecute method; 3. Execute the doinbackground method; 4. Execute the onpostexecute method.
*
* @ Author Samsung
*
*/
Public class firstasynctask extends asynctask <integer, integer, string> {
Private textview = NULL;
Private progressbar = NULL;
Public firstasynctask (textview, progressbar ){
This. textview = textview;
This. progressbar = progressbar;
}
/**
* This method is not running in the UI.
*/
@ Override
Protected string doinbackground (integer... arg0 ){
Threadoperate = new threadoperate ();
Int I = 0;
For (I = 10; I <= 100; I ++ ){
Threadoperate. Operate ();
Publishprogress (I); // call the onprogressupdate Method
}
Return I + arg0 [0]. intvalue () + "";
}
/**
* This method runs in the UI.
*/
@ Override
Protected void onpreexecute (){
Textview. settext ("start to execute asynchronous operations ...... ");
}
/**
* This method runs in the UI.
*/
@ Override
Protected void onpostexecute (string result ){
Textview. settext ("Asynchronous Operation execution ends ...... "+ Result );
}
/**
* This method runs in the UI.
*/
@ Override
Protected void onprogressupdate (integer... values ){
Int value = values [0];
System. Out. println ("Forward:" + value );
Progressbar. setprogress (value );
}
}
Package com. duoguo. Android;
/**
* Thread
*
* @ Author Samsung
*
*/
Public class threadoperate {
Public void operate (){
Try {
Thread. Sleep (1*1000 );
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
}
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical">
<Textview
Android: Id = "@ + ID/TV _tip"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
<Progressbar
Android: Id = "@ + ID/pb_progress"
Style = "? Android: ATTR/progressbarstylehorizontal"
Android: layout_width = "200dip"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center"
Android: max = "100" type = "codeph" text = "/codeph"/>
<Button
Android: Id = "@ + ID/btn_start"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/start"/>
</Linearlayout>