Sixth chapter Asynctask Asynchronous loading
1. Benefits: You do not need to create threads to manage threads
Cons: More Steps
2. Steps:
(1) To create a class inheritance asynctask<xxx,xxx,xxx>;
Three generic parameters:
The first: determines the type of incoming value for the Execute () method, which determines the value type of the Doinbackground () method.
The second: determines the incoming value type of the Publishprogress () method, which determines the value type of the Onprogressupdate () method
The third: Determines the return value type of the Doinbackground () method, determines the value type of the OnPostExecute () method passed in,
3. Execution Process:
Onpreexecute→→→doinbackground (in child threads) →→→onpostexecute
↓
↓
↓
Publishprogress→→→onprogressupdate
4. Generic type:
New features after Java5.0, variable parameters, type certain, number of variables
For example:
static int Add (INTEGER...C) {
int sum=0;
for (int i=0;i<c.length;i++) {
Sum+=c[i];
}
return sum;
}
5. Download the network resources step:
Get the path
String path = xxx;
Get URL Object
URL url = new URL (path);
Open link
URLConnection urlconnection = Url.openconnection ();
Get stream
InputStream is = Urlconnection.getinputstream ();
Bitmap types of objects are converted by the picture factory
Bitmap Bitmap = Bitmapfactory.decodestream (IS);
Setting the connection timeout
Urlconnection.setconnecttimeout (3000);
Set Read timeout
Urlconnection.setreadtimeout (3000);
Android Learning note-sixth Asynctask asynchronous loading