Use of Android Asynctask

Source: Internet
Author: User

The following is only my humble opinion, the great god please ignore ~

Asynctask is a very handy tool class for writing asynchronous tasks, sometimes blocking when doing network operations, and the user experience is very poor.

Of course, the thread can solve this problem, but I personally feel that Asynctask is more convenient to use, of course, some people say that Asynctask is flawed, but I have not encountered so-called defects.

Nonsense not much to say, the use of Asynctask:

1 classDemoThreadextendsAsynctask<string, String, string>2 {3ProgressDialog M_pdialog =NULL;4 @Override5 protectedString Doinbackground (String ... arg0) {//Background Execution Note: There can be no foreground operation here6List<namevaluepair> userparms =NewArraylist<namevaluepair>();7Httputils Utils =Newhttputils ();8String result = Utils.dopost ("http://www.baidu.com/", userparms);9     returnresult;Ten } One @Override A protected voidOnPreExecute () {//Initialize -M_pdialog =NewProgressDialog (mainactivity. This); - M_pdialog.setprogressstyle (progressdialog.style_spinner); theM_pdialog.setmessage ("Please wait ..."); -M_pdialog.setindeterminate (false); -M_pdialog.setcancelable (true); - m_pdialog.show (); + } - @Override + protected voidOnPostExecute (String result)//return value for background A { at     if(M_pdialog! =NULL&&m_pdialog.isshowing ()) - { - m_pdialog.hide (); - M_pdialog.dismiss (); - } - Textview1.settext (result); in } -  to}

Look at the code wearies, click here: Asynctask_demo.zip

Use of Android Asynctask

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.