Use of AsyncTask and AsyncTask

Source: Internet
Author: User

Use of AsyncTask and AsyncTask

1 --- network Permissions

2 -- the main thread calls the AsyncTask class instance call

/**
* 1 MAsyncTask is instantiated in the main thread;
* 2 doInBackground in the subthread, the data requested by the subthread will be sent to the method (onPostExecute) running in the main thread)
* 3 the underlying principle is Handler + Thread;
*/
New masynctask(cmd.exe cute (urlString );

3 --- compile the AsyncTask class;

/*** Create an asynchronous task class * The first generic --- is the passed interface address * The second parameter is the request interface progress * The third parameter is the data returned after the asynchronous task is processed */class MAsyncTask extends AsyncTask
 
  
{Public MAsyncTask () {super ();}/*** String... the strings parameter is a variable parameter. * This method runs in the sub-thread to request the network. */@ Override protected String doInBackground (String... strings) {String url = strings [0]; String netjson = NetUtils. getNetjson (url); // return the json from the network request; Log. e ("wzq", "doInBackground:" + netjson); return netjson;}/*** the parameter s received by this method is the value returned by doInBackground; * This method runs in the main thread and updates the ui. ** @ param s */@ Override protected void onPostExecute (String s) {super. onPostExecute (s); // This parameter s is the returned json TV. setText (s);}/*** network access is time-consuming. This method is used to update the ui progress; * It is also run in the main thread; this method will be called back multiple times; * @ param values */@ Override protected void onProgressUpdate (Void... values) {super. onProgressUpdate (values); // calculate the current value; Calculate a percentage; update the progress bar ui ;}}
 

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.