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