异步任务AsyncTask使用解析

来源:互联网 发布:比特彗星怎么设置网络 编辑:程序博客网 时间:2024/04/28 16:43

一、解析
1.异步任务AsyncTask主要是用来更新UI线程,比较耗时的操作可以在AsyncTask中使用。

2.AsyncTask是个抽象类,使用时需要继承这个类,然后调用execute()方法。注意继承时需要设定三个泛型Params,Progress和Result的类型,如:

AsyncTask<Void,Inetger,Void>

(1)Params是指调用execute()方法时传入的参数类型和doInBackgound()的参数类型
(2)Progress是指更新进度时传递的参数类型,即publishProgress()和onProgressUpdate()的参数类型
(3)Result是指doInBackground()的返回值类型

3.AsyncTask主要有以下几个方法:
(1)doInBackgound() 这个方法是继承AsyncTask必须要实现的,运行于后台,耗时的操作可以在这里做。
(2)publishProgress() 更新进度,给onProgressUpdate()传递进度参数
(3)onProgressUpdate() 在publishProgress()调用完被调用,更新进度

以上是AsyncTask使用方法的简单介绍,然后在贴出一个实例供大家参考

MainActivity代码

public class MainActivity extends AppCompatActivity {    private Button mButton;    private TextView mTextView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        init();    }    private void init() {        mButton = (Button)findViewById(R.id.btn);        mTextView = (TextView)findViewById(R.id.text);        mButton.setOnClickListener(mOnClickListener);    }    View.OnClickListener mOnClickListener = new View.OnClickListener() {        @Override        public void onClick(View v) {            update();        }    };    private void update() {        UpdateTextTask mUpdateTextTask = new UpdateTextTask(MainActivity.this,mTextView);        mUpdateTextTask.execute();    }}

activity_main.xml布局代码:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#FFFFFF">    <Button        android:id="@+id/btn"        android:text="开始"        android:layout_width="match_parent"        android:layout_height="50dp" />    <TextView        android:id="@+id/text"        android:layout_centerInParent="true"        android:text="点击开始我就跑起来了"        android:textSize="30sp"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></RelativeLayout>

然后是AsyncTask的代码:

/** * @author :huangxianfeng on 2016/11/11. * 异步任务的简单使用 */public class UpdateTextTask extends AsyncTask<Void,Integer,Integer> {    private Context mContext;    private TextView mTextView;    public UpdateTextTask(Context mContext, TextView mTextView) {        this.mContext = mContext;        this.mTextView = mTextView;    }    //运行在UI线程中,在调用doInBackground()之前执行    @Override    protected void onPreExecute() {        super.onPreExecute();        Toast.makeText(mContext, "开始执行", Toast.LENGTH_SHORT).show();    }    //后台运行的方法,可以运行非UI线程,可以执行耗时的方法    @Override    protected Integer doInBackground(Void... params) {        int i=0;        while(i<100){            i++;            publishProgress(i);            try {                Thread.sleep(1000);            } catch (InterruptedException e) {            }        }        return null;    }    //运行在ui线程中,在doInBackground()执行完毕后执行    @Override    protected void onPostExecute(Integer integer) {        super.onPostExecute(integer);    }    //在publishProgress()被调用以后执行,publishProgress()用于更新进度    @Override    protected void onProgressUpdate(Integer... values) {        super.onProgressUpdate(values);        mTextView.setText("" + values[0]);    }}

以上是这个简单小demo的全部内容,仅供大家参考学习。

0 0
原创粉丝点击