异步任务AsyncTask使用解析

一、解析
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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DT从零到壹

您的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值