1. Threading Thread
2. Asynchronous Task Asynctask
-------------------------------------------------
-------------------------------------------------
1. Threading Thread
After the apk is turned on, there will be a main thread that is responsible for interacting with the user. If a time-consuming operation is performed in the main thread, the interface stops responding, so the time-consuming operation is transferred to another thread
2. Asynchronous Task Asynctask
Time consuming tasks cannot be performed in the main thread of the UI, asynchronous tasks use Asynctask
Asynctask<string, Float, string>
The first parameter, string, represents the passed-in parameter
The second parameter, float, represents the value in the execution process
The third parameter, string, represents the return value
Here is a demo that reads the Web page operation
① Layout File: Click button to place the captured content in the TextView display
<ButtonAndroid:id= "@+id/btnread"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "@string/read_website" /><ScrollViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content" > <TextViewAndroid:id= "@+id/textview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world" /></ScrollView>
② definition Asynctask
You cannot invoke UI controls in Doinbackground to manipulate
The action on the UI control must be performed in several other callback functions:oncancelled,oncancelled (),onpostexecute, onpreexecute, onprogressupdate
Public voidreadurl (String url) {New asynctask<string, Float, string>() { //The first parameter, string, represents the passed -in parameter//the second parameter, float, represents the value in the execution process//The third parameter, string, represents the return value@Overrideprotectedstring Doinbackground (String ... params) {//running in the background Try{URL URL=NewURL (params[0]); URLConnection Conn=url.openconnection (); LongTotal = Conn.getcontentlength ();//total length of content to readInputStream is =Conn.getinputstream (); InputStreamReader ISR=NewInputStreamReader (IS); BufferedReader BR=NewBufferedReader (ISR); String Line; StringBuilder Builder=NewStringBuilder (); while(line = Br.readline ())! =NULL) {builder.append (line); floatValues = builder.tostring (). Length ()/Total ; publishprogress (values); //corresponding to the Onprogressupdate, external release Progress} br.close (); Isr.close (); Is.close (); return builder.tostring ();//Correspondence OnPostExecute }Catch(malformedurlexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } return NULL; } @Overrideprotected voidoncancelled () {//TODO auto-generated Method Stub Super. oncancelled (); } @Overrideprotected voidoncancelled (String result) {//TODO auto-generated Method Stub Super. oncancelled (Result); } @Overrideprotected voidOnPostExecute (String result) {//when the current async execution is complete, it will be recalled.Tv1.settext (Result); Super. OnPostExecute (Result); } @Overrideprotected voidOnPreExecute () {//It will be recalled before the current async execution is complete.Toast.maketext (Getapplicationcontext (), "Start Read", Toast.length_short). Show (); Super. OnPreExecute (); } @Overrideprotected voidonprogressupdate (Float ... values) {//publish the progress of execution in the process of executing a taskLOG.D ("Carloz", "values =" + values[0]); Super. Onprogressupdate (values); }}.execute (URL);}
③ Call Asynctask
TextView TV1; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TV1=(TextView) Findviewbyid (R.ID.TEXTVIEW1); Findviewbyid (R.id.btnread). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) { Readurl ( "Http://carloz.duapp.com"); } });}
The results are as follows:
Android Thread and Asynctask