Android's asynchronous call

  • Overview of
    action AsyncTask can run a good, accurate use of the UI thread, he can be a time-consuming (a few seconds) in the background, and can return the results to the UI thread, without going through (Thread and Handler Operation operation ).
    Must create a subclass of AsyncTask, rewrite at least when using its doInBackground (as the name suggests, how you want to perform an operation in the background) method, most will rewrite onPostExcute (after completion of the background you want to return a result UI thread where) method.
  • AsyncTask generic definition of three and four steps
    to first understand the three kinds of generic AsyncTask defined as:

    the Params: Parameter Type issued tasks when executed background operation, i.e. doInBackground process parameters, for example: if the download performing a background image the document, here is imagePath String type.
    Progress: Type backstage operating process execution schedule.
    Result: return to the data type of the UI thread. That argument onPostExcute method, for example: if the background is a picture download task here will certainly return a Bitmap type to the UI thread.
    Of course, not all types will be used to, for example, Progress, if you do not show progress bar will not need this type of design, the direct use of Void type.
    Second, understand the four steps:
    onPreExcute: do background tasks to perform before the operation, for example, can pop up a Dialog user is prompted to download. The Dialog dismiss also need in onPostExcute process.
    onInBackground: slightly. Previously told. NOTE: This method can be used publishProgress release schedule unit, and the unit will be released in progress onProgressUpdate step.
    onProgressUpdate: As the name suggests it is the update operation progress bar. The progress of the unit displayed in the UI thread.
    onPostExcute: slightly. Added: parameter to this method is the result of doInBackground method returns.
  • AsyncTask normal operation conditions:
    ① AsyncTask class must be loaded in the UI thread.
    ② task instances must be created in the UI thread.
    To sum up the previous two is to create a subclass of AsyncTask in the UI thread, and must be instantiated in the UI thread.
    ③ call excute the UI thread () to perform the task. Do not manually invoke the function AsyncTask of four steps.
  • Why doInBackground method call publishProgress, the progress bar may be updated in real time onProgressUpdate.
    Because before calling publishProgress, any memory effect doInBackground for onProgressUpdate is visible, and the subsequent publishProgress onProgressUpdate will not affect the ongoing operation.

  • The following picture shows an asynchronous load operation
    ① preparatory work
    "to access the network picture, you first need to grant permission. Manifest.xml in Add the following statement before the node APP open access to the network

    "network request to download the picture needs to HttpClient class, but is no longer supported HttpClient after Android 6.0, but still can be used, you need to add the following code in the APP build.gradle in:

Guess you like

Origin www.cnblogs.com/21-T/p/10940179.html