Asynctask (asynchronous Task) explaining-android learning Journey (46)

Source: Internet
Author: User

Asynctask Introduction The Android UI thread mainly handles user keystrokes, touch screens and view rendering, and cannot handle time-consuming operations inside, otherwise the ANR will occur, so the time-consuming operation is to open a single thread, but the new thread cannot directly handle the view of the UI thread.

How to use Asynctask

code example

This instance has only one button and text box, click the button to download data from the network, and then display it in the text box.

 PackagePeng.liu.test;;;ImportAndroid.content.Context;ImportAndroid.os.AsyncTask;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.TextView;;;;;; Public  class mainactivity extends Activity{    PrivateTextView Tvasync;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);    Tvasync = (TextView) Findviewbyid (; } Public void Download(View Source)throwsmalformedurlexception {Downtask Downtask =NewDowntask ( This); Downtask.execute (NewURL ("")); } class Downtask extends asynctask<url,integer,string>{progressdialog Dialog;intHasData Context context; Public Downtask(Context CTX) { This. context = CTX; }@Override        protectedStringDoinbackground(URL ... URLs) {StringBuilder SB =NewStringBuilder ();Try{URLConnection conn = urls[0].openconnection (); BufferedReader BF =NewBufferedReader (NewInputStreamReader (Conn.getinputstream (),"Utf-8")); String line =NULL; while(line = Bf.readline ())! =NULL) {sb.append (line);                    hasdata++;                Publishprogress (HasData); }returnSb.tostring (); }Catch(Exception e)            {E.printstacktrace (); }return NULL; }@Override        protected void OnPostExecute(String s)            {Tvasync.settext (s);        Dialog.dismiss (); }@Override        protected void OnPreExecute() {dialog =NewProgressDialog (context); Dialog.settitle ("Hello"); Dialog.setcancelable (false); Dialog.setmessage ("World"); Dialog.setmax (202);            Dialog.setprogressstyle (progressdialog.style_horizontal); Dialog.setindeterminate (false); (); }@Override        protected void onprogressupdate(Integer ... values) {Tvasync.settext (hasdata+""); Dialog.setprogress (values[0]); }    }}

Asynctask (asynchronous Task) explaining-android learning Journey (46)

Related Article

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: 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.