zoukankan      html  css  js  c++  java
  • AsyncTask

    AsyncTask


    AsyncTask的设计目标是比较短的操作(最多几秒),如果希望执行更长时间,使用 java.util.concurrent 中的 Executor, ThreadPoolExecutor, FutureTask更合适。

    Asyntask的泛型实现:

    • Params:发送给Asyntask的执行参数类型
    • Progress:the type of the progress units published during the background computation
    • Result: background计算阶段的结果类型

    Asyntask执行中的四步:

    • onPreExecute() : 任务执行前执行,一般用途例如显示加载框,进度条
    • doInBackground(Params...) : 执行后台任务(途中可更新进度条)
    • onProgressUpdate(Progress...) : 更新进度条
    • onPostExecute(Result) :

    注意点

    • Asyntask实例必须在UI线程中简历
    • execute(Params...)必须在UI线程中调用。
      new DownloadFilesTask().execute(urlStr);
    • 不要手动调用onPreExecute(), onPostExecute()..等方法
    • The task can be executed only once(an exception will be thrown if a second execution is attempted)

    AsyncTask guarantees that all callback calls are synchronized in such a way that the following operations are safe without explicit synchronizations.

    • Set member fields in the constructor or onPreExecute(), and refer to them in doInBackground(Params...)
    • Set member fields in doInBackground(Params...), and refer to them in onProgressUpdate(Progress...) and onPostExecute(Result)
  • 相关阅读:
    oracle数据库常用指令
    MySql常用命令
    js动态添加和删除行
    Mybatis模糊查询
    laravel 成功跳转页面
    laravel 验证码
    git获取公钥和私钥以及常用的命令
    laravel css和js的引入
    git add -A 、git add -u 、 git add . 三种区别
    windows下github 出现Permission denied (publickey).解决方法
  • 原文地址:https://www.cnblogs.com/weilf/p/5210869.html
Copyright © 2011-2022 走看看