Android Asynctask Asynchronous task

Source: Internet
Author: User



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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.