Android Learning note-sixth Asynctask asynchronous loading

Source: Internet
Author: User

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

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.