3.2异步消息处理机制-AsyncTask

2018-11-09  本文已影响0人  205蚁

AsyncTask详解

1.什么事AsyncTask

2.AsyncTask的使用方法

public class UpdateInfoAsyncTask extends AsyncTask<Integer,Integer,String>{
            
            protected void onPreExecute(){
                textView.setText("开始执行异步框架");
            }
            protected String doInBackground(Integer...params){//为什么是可变参数,说明可以传入过个参数,但是类型已经确定
                int i = 0;
                for(i=10;i<=100;i+=10){
                    publicProgress(i);
                }
                return i+ params[0].intValue()+"";
                
            }
            
            protected void onProgressUpdate(Integer... values){
                int value = values[0];
                progessBar.setProgess(value);
            }
            protected void onPostExecute(String result){
                textView.setText("异步操作执行结束"+result); 
            }
}
参数1:执行操作传入
参数2:进度 返回
参数3:结果返回

onPreExecute 在UI线程中,开启进度条,提示等
doInBackground 在线程池中
    publicProgress(i)会将参数 回调到onProgressUpdate
onPostExecute 回到UI线程中,处理结果  

3.AsyncTask内部原理

4.AsyncTask的注意事项

上一篇 下一篇

猜你喜欢

热点阅读