AsyncTask3参数以及6方法浅析

大家都知道,AsyncTask是用来进行异步加载的,当然还有一种方式是Handler,此处就先不对Handler进行说明

首先先举个小例子来对异步,同步进行一个说明:



首先来看看官网上的源码:

<p>AsyncTask enables proper and easy use of the UI thread. This class allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.</p>
以上翻译:

AsyncTask使适当的和容易使用的UI线程。这个类允许您执行后台操作和发布结果在UI线程上无需操纵线程或Handler进行处理


AsyncTask is designed to be a helper class around {@link Thread} and {@link Handler} and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs
以上翻译:
AsyncTask被设计成一个帮助类,围绕线程和handle
不建议做耗时操作(不要做时间太久的操作),如果你要保持一个线程运行在长时间内,建议使用不同的api,自己通过线程池搭建

An asynchronous task is defined by a computation that runs on a background thread and
 whose result is published on the UI thread.
以上翻译:
一个异步任务可以跑在后台线程中,结果会被发布到UI线程

AsyncTask有三个参数,六个方法,然后对这三个参数,六个方法进行说明
三个参数: Params,Progress,Result
//参数一:Params:execute传递
//参数二:Progress:进度
//参数三:Result是doInBackground的返回值,在onPostExecute处进行接收

六个方法:onPreExecute,doInBackground,publishProgress,onPostExecute,onProgressUpdate,execute
* execute:执行任务
        * doInBackground: 执行线程:加载数据:  run UI线程里面
        * onPreExecute: 执行线程之前:更新UI:run 非UI线程
        * onPostExecute: 执行完线程之后:更新UI,run UI线程
        * publishProgress
        * onProgressUpdate
new AsyncTask<String,Integer,Boolean>(){
            //Boolean是参数三Result,传递到onPostExecute的参数中
	   // 执行线程:加载数据:run 非UI线程里面
            @Override
            protected Boolean doInBackground(String... params) {
                Log.d("heima12",params[0]);//hello
                Log.d("heima12",params[1]);//xiaohei

                }

                return null;//此处返回true,在onPostExecute的result处进行接收
            }

        }.execute("hello","xiaohei");//此处可以传递多个值,传递"hello","小黑",在doInBackground的(String...Params)进行接收,接收处是可变参数


执行以上代码打印Log:



	    // 执行线程之前:更新UI
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

 		@Override
            protected Boolean doInBackground(String... params) {
                }
	return null;//此处返回true,在onPostExecute的result处进行接收

	// 执行完线程之后:更新UI
 	//该方法的参数类型与doInBackground方法中Boolean的返回值类型一致
            @Override
            protected void onPostExecute(Boolean result) {
                Log.d("heima12",""+result);//true
            }


执行以上代码打印Log:


	    //Boolean是参数三Result,传递到onPostExecute的参数中
            // 执行完线程之后:更新UI
            @Override
            protected Boolean doInBackground(String... params) {
                Log.d("heima12",params[0]);//hello
                Log.d("heima12",params[1]);//小黑

                for (int i = 0; i <100; i++) {
                    SystemClock.sleep(100);
                    //当有这样一个场景,比如说下载时,后台线程还没有执行完,要通知更新UI,通知进度变化,提供这样一个方法publishProgress
                    publishProgress(i/*,10*/);//参数是可变参数,i是Progress,如果是下载,下载一点,发布一点,在onProgressUpdate处进行接收
                }

                return true;//此处返回true,在onPostExecute的result处进行接收
            }

//接收从publishProgress传递过来的数据
@Override
protected void onProgressUpdate(Integer... values) {
    Toast.makeText(MainActivity.this, "i="+values[0], Toast.LENGTH_SHORT).show();
}



执行以上代码弹出Toast




以上就是对AsyncTask的中3参数以及6方法浅析.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值