Android AsyncTask

一、使用步骤

1.自定义MyAsyncTask类继承AsyncTask 
2.定义AsyncTask的三种泛型参数
3.重写方法相关方法
4.调用execute方法执行异步任务

二、AsyncTask方法

1.onPreExecute()在任务执行前执行

2.doInBackground()执行任务

3.onProgressUpdate()根据doInBackground方法传递的值更新当前状态

4.onPostExecute()任务完成后业务处理

三、下载图片测试

activity_main2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2"
    android:id="@+id/MainActivity2"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="11"
        tools:srcCompat="@tools:sample/avatars" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="TextView" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="点击下载" />

</LinearLayout>

 

MainActivity2.java:

public class MainActivity2 extends AppCompatActivity {

    static ImageView imageView;
    static TextView textView;
    static Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        button = findViewById(R.id.button2);
        imageView = findViewById(R.id.imageView);
        textView = findViewById(R.id.textView2);
        textView.setText("");
        button.setOnClickListener(view -> {
            MyAsyncTask myAsyncTask = new MyAsyncTask(this);
            myAsyncTask.execute("图片");
        });
    }
}

MyAsyncTask.java:

public class MyAsyncTask extends AsyncTask<String, Integer, String> {

    private ProgressDialog progressDialog;
    private Context context;


    public MyAsyncTask(Context context) {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(context);
        progressDialog.setTitle("正在下载中,请稍后...");
        //设置ProgressDialog样式为圆圈的形式
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.show();

    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        MainActivity2.textView.setVisibility(View.VISIBLE);
        MainActivity2.textView.setText(s);
        //关闭进度条
        progressDialog.dismiss();
        MainActivity2.imageView.setImageResource(R.drawable.ic_launcher_background);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        //跟新进度条
        progressDialog.setProgress(values[0]);

    }

    @Override
    protected String doInBackground(String... strings) {
        //execute()方法传递的参数
        String content = strings[0];
        int flag = 0;
        while (flag < 100) {
            int random = new Random().nextInt(20) + 1;
            flag += random;
            //跟新进度,将回调onProgressUpdate()方法
            publishProgress(flag);
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        return "下载" + content + "成功!";
    }

}

运行截图: 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值