安卓AsyncTask 使用方法

虽然handler提供了某些多线程方法 但是acynctask 提供了更多的相关方法 比如处理进度条 或者其他多线程相关问题的类 可以更好的处理多线程 而且这个地方不太好理解 涉及泛型(c++里面叫模板)和多个参数传参 等等 要好好记录下 部分内容参考下面链接
http://www.cnblogs.com/devinzhang/archive/2012/02/13/2350070.html

AsyncTask定义了三种泛型类型 Params,Progress和Result。

  • Params 启动任务执行的输入参数,比如HTTP请求的URL。
  • Progress 后台任务执行的百分比。
  • Result 后台执行任务最终返回的结果,比如String。

大概的意思就是

class MyAsyncTask extends AsyncTask<String,Integer,String>

{

    //这里的string params的string和传入的第一个参数有关

    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
    
        return null;
    }

}

注意1 这里使用了泛型 需要传入具体的类模板 就是要传入三个变量的数据类型 没有的话传void(不是null null不是数据类型)

这个地方很难一两句话说明白 以后不懂了再去看java的泛型吧

注意2这里String... params 是java一种特殊写法 表示不缺定的参数个数的字符串 其实就是可以传入多个参数的可以不用形成数组的一种简便写法

相当与

String[] s;

public String f(String[] temp)

然后讲s传入一样 下面是demo

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        exec("一个参数");
        exec("第一个参数","第二个参数");
    }

    public static void exec(String...strings)
    {
        String[] s=strings;
        for(int i=0;i<s.length;i++)
        {
            System.out.println(s[i]);
        }
        System.out.println("-----------");
    }
}

官网上写的使用这个类的四个步骤

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.

大概意思如下(就不翻译英语了 鸟语太差)

1.onPreExecute(), 在执行线程之前的初始化工作(看名字pro 。。之前)

2doInBackground(Params...) 在后台做工作 其实就是执行一个新的线程去干一些消耗资源的活 可以在这个函数内调用publishProgress来提示更新进度()

3.onProgressUpdate(Progress...) 具体更新进度()其实就是类似下载条 下载了多少之类的 可以在此处操作processbar 进度条来或者各种view 比如直接创建一个testview显示 66/100 之类的

4onPostExecute(Result) 任务完成之后的执行的更新ui的操作 比如下载图片完成了显示图片之类的

补充下:这四个函数 其中doinbackgroud不可以更新ui 其他几个可以更新ui

最后就是执行 new MyAsyncTask().execute("","","");//随便几个参数都行 反正他也打包成参数数组传入 大概这样 其他用的时候查api就好
原文地址:https://www.cnblogs.com/yujiaao/p/3603693.html