Asynctask<params, Progress, result>
Params: Type of input parameter to start task execution
Progress: Progress value type for background task completion
Result: The type of the result returned after the background execution task finishes
Use Asynctask as long as the following three steps:
Doinbackground (Params ... Overriding this method is the task that the background thread is about to complete, and the method can call the Publishprogress () method to update the execution progress of the task.
Onprogressupdate (Progress ... The method is fired when the Publishprogress () method is called in the Doinbackground () method to update the execution progress of the task.
OnPreExecute (): This method is called before the background time-consuming operation is performed. Typically this method is used to complete some initialization preparations
OnPostExecute (Result): When Doinbackground () completes, the method is automatically called and the return value of the Doinbackground () method is passed to the method.
public class Mainactivity extends Activity {
Private EditText et;
Private Button BT;
Private TextView TV;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
ET = (EditText) Findviewbyid (R.ID.EDITTEXT1);
BT = (Button) Findviewbyid (R.id.button1);
TV = (TextView) Findviewbyid (R.ID.TEXTVIEW1);
Bt.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View arg0) {
Downtask task = new Downtask (mainactivity.this);
try {
Task.execute (New URL ("http://www.crazyit.org/ethos.php"));
} catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
});
}
Class Downtask extends Asynctask<url, Integer, string> {
ProgressDialog Dialog;
Defines the number of records that have been read
int hasread = 0;
Context Mcontext;
Public Downtask (Context c) {
Mcontext = C;
}
@Override
protected void OnPostExecute (String result) {
Tv.settext (result);
Dialog.dismiss ();
}
@Override
protected void OnPreExecute () {
dialog = new ProgressDialog (mcontext);
Dialog.settitle ("Multi-tasking in progress");
Dialog.setmessage ("Multitasking is executing please wait ... ");
Dialog.setcancelable (FALSE);
Dialog.setmax (202);
Dialog.setprogressstyle (progressdialog.style_horizontal);
Dialog.setindeterminate (FALSE);
Dialog.show ();
}
@Override
protected void Onprogressupdate (Integer ... values) {
Tv.settext ("already read" + values[0] + "line");
Dialog.setprogress (Values[0]);
}
@Override
Protected String doinbackground (URL ... arg0) {
StringBuilder sb = new StringBuilder ();
try {
URLConnection conn = Arg0[0].openconnection ();
BufferedReader br = new BufferedReader (New InputStreamReader (
Conn.getinputstream (), "Utf-8"));
String line = null;
while (line = Br.readline ()) = null) {
Sb.append (line + "\ n");
hasread++;
Publishprogress (Hasread);
}
return sb.tostring ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return null;
}
}
}
Android Asynctask Asynchronous task