多线程——AsyncTask类的使用(一)

When an asynchronous task is executed, the task goes through 4 steps:

  1. onPreExecute(), invoked on the UI thread before the task     is executed. This step is normally used to setup the task, for instance by     showing a progress bar in the user interface.
  2. doInBackground(Params...), invoked on the background thread     immediately after onPreExecute() finishes executing. This step is used     to perform background computation that can take a long time. The parameters     of the asynchronous task are passed to this step. The result of the computation must     be returned by this step and will be passed back to the last step. This step     can also use publishProgress(Progress...) to publish one or more units     of progress. These values are published on the UI thread, in the      onProgressUpdate(Progress...) step.
  3. onProgressUpdate(Progress...), invoked on the UI thread after a     call to publishProgress(Progress...). The timing of the execution is     undefined. This method is used to display any form of progress in the user     interface while the background computation is still executing. For instance,     it can be used to animate a progress bar or show logs in a text field.
  4. onPostExecute(Result), invoked on the UI thread after the background     computation finishes. The result of the background computation is passed to     this step as a parameter.

当在主线程中调用new MyTask().execute(args)时,触发

execute()将参数传给doInBackground()

doInBackground()返回值传给onPostExecute()

在doInBackground()中调用的publishProgress()的参数,传递给onProgressUpdate()

多线程——AsyncTask类的使用(一)

时间: 2024-08-01 22:44:54

多线程——AsyncTask类的使用(一)的相关文章

多线程AsyncTask中使用Jsoup 报错Caused by: java.lang.NoClassDefFoundError: org.jsoup.Jsoup

代码如下,在多线程AsyncTask类中的doInBackground调用Jsoup protected String doInBackground(String... params) { // TODO Auto-generated method stub Log.i(TAG, "doInBackground(Params... params) called"); try { Document doc=Jsoup.connect(params[0]).get(); Elements

Android中多线程编程(四)AsyncTask类的详细解释(附源码)

Android中多线程编程中AsyncTask类的详细解释 1.Android单线程模型 2.耗时操作放在非主线程中执行 Android主线程和子线程之间的通信封装类:AsyncTask类 1.子线程中更新UI 2.封装.简化异步操作. 3.AsyncTask机制:底层是通过线程池来工作的,当一个线程没有执行完毕,后边的线程是无法执行的.必须等前边的线程执行完毕后,后边的线程才能执行. AsyncTask类使用注意事项: 1.在UI线程中创建AsyncTask的实例 2.必须在UI线程中调用As

Android 多线程-----AsyncTask详解

本篇随笔将讲解一下Android的多线程的知识,以及如何通过AsyncTask机制来实现线程之间的通信. 一.Android当中的多线程 在Android当中,当一个应用程序的组件启动的时候,并且没有其他的应用程序组件在运行时,Android系统就会为该应用程序组件开辟一个新的线程来执行.默认的情况下,在一个相同Android应用程序当中,其里面的组件都是运行在同一个线程里面的,这个线程我们称之为Main线程.当我们通过某个组件来启动另一个组件的时候,这个时候默认都是在同一个线程当中完成的.当然

AsyncTask类插入数据到服务器与接口回调

////////////////2016/04/21///////////////////// //////////////by XBW/////////////////////////// ///////////环境  api22 eclipse ///////////// 搞了这么久终于弄好了接口,之前都是一个人在做项目,自己随心所欲的写代码,想怎么写就怎么写,到了团队呢,这接口那接口,各种类,各种枚举,各种内部类,抽象类,单例懒汉,单例饿汉的,也算学了不少东西, 我做的是把数据插入到数据库

AsyncTask类浅析

使用AsyncTask类遵守的准则: 1  Task的实例必须在UI thread中创建: 2  Execute方法必须在UI thread中调用:3  不要手动的调用onPfreexecute(),onPostExecute(result)Doinbackground(params-),onProgressupdate(progress-)这几个方法:4 该task只能被执行一次,否则多次调用时将会出现异常;AsyncTask的整个调用过程都是从execute方法开始的,一旦在主线程中调用ex

2.匿名类,匿名类对象,private/protected/public关键字、abstract抽象类,抽象方法、final关键字的使用,多线程Thread类start方法原理

package com.bawei.multithread; //注意:模板方法我们通常使用抽象类或者抽象方法!这里我们为了方便在本类中使用就没有使用抽象类/抽象方法 public class TemplateThread { //如果这个方法不想被子类或者别人随意改动[这样子类就不能覆写该方法了],这里方法就要设置为final方法 public final void println(String message){ System.out.println("###################

Java多线程——ThreadLocal类

一.概述 ThreadLocal是什么呢?其实ThreadLocal并非是一个线程的本地实现版本,它并不是一个Thread,而是threadlocalvariable(线程局部变量).也许把它命名为ThreadLocalVar更加合适.线程局部变量(ThreadLocal)其实的功用非常简单,就是为每一个使用该变量的线程都提供一个变量值的副本,是Java中一种较为特殊的线程绑定机制,是每一个线程都可以独立地改变自己的副本,而不会和其它线程的副本冲突. 从线程的角度看,每个线程都保持一个对其线程局

java下的多线程操作工具类(原创)

因为毕业设计上需要将控制台的任务实时输出到界面上, 而且那是个Hadoop的mapreduce任务,一跑半个小时的节奏,所以需要用到多线程并随时读取返回的数据, 百度了没找到合适的方法,于是被逼无奈编写了这个十分简单的多线程操作工具类. 大概的功能就是可以执行一个特定的线程(线程必须调用本类的方法才能实现实时输出信息的功能),然后随时调用readResult方法就可以获取到结果集. 在毕业设计的应用上,就是创建一个工具类,然后让它自己去跑,根据taskId保存到session中,用ajax循环读

curl多线程下载类

<?php /** * curl多线程下载类 */class MultiHttpRequest{ public $urls = array (); private $res = array (); private $curlopt_header = 0; private $method = "GET"; private $curlopt = array (); public function __construct($urls = false, $curlopt = array