首页 技术 正文
技术 2022年11月8日
0 收藏 324 点赞 1,881 浏览 5110 个字

Android提供了异步处理消息的方式大致有两种,第一种是handler+Thread,之前已经对于这种方式做过分析,第二种就是AsyncTask,这是Android1.5提供的一种轻量级的工具类,其本质也是对handler和Thread进行了封装适用于一些简单的异步处理。

AsyncTask是一个抽象类,我们需要继承他并实现他的方法,其中有4个方法比较重要,对应着异步处理的几个过程。分别是:

  • onPreExecute()

  • doInBackground(Params… var1)
  • onProgressUpdate(Progress… values)
  • protected void onPostExecute(Result result)

这四个方法的一些参数和返回值都是基于泛型的,而且泛型的类型还不一样,所以在AsyncTask的使用中会遇到三种泛型参数:Params, Progress 和 Result。
  • Params表示用于AsyncTask执行任务的参数的类型
  • Progress表示在后台线程处理的过程中,可以阶段性地发布结果的数据类型
  • Result表示任务全部完成后所返回的数据类型

我们通过调用asynctask的excute()方法就可以执行异步任务,会按照如上的顺序执行,下面依次来介绍一下这几个方法。

onPreExecute()

在执行任务之前在UI线程上调用。此步骤通常用于设置任务,例如通过在用户界面中显示进度条。

doInBackground(Params… var1)

onPreExecute()完成执行后立即在后台线程上调用。此步骤用于执行可能需要很长时间的后台计算。异步任务的参数将传递给此步骤。计算结果必须由此步骤返回,并将传递回最后一步。此步骤还可用于publishProgress(Progress...)发布一个或多个进度单位。这些值在onProgressUpdate(Progress...)步骤中发布在UI线程上 。

onProgressUpdate(Progress… values)

在调用后在UI线程上调用publishProgress(Progress...)。执行的时间是不确定的,如果在doInBackground中多次调用了publishProgress方法,那么主线程就会多次回调onProgressUpdate方法。此方法用于在后台计算仍在执行时显示用户界面中的任何形式的进度。例如,它可用于为进度条设置动画或在文本字段中显示日志。

protected void onPostExecute(Result result)

在后台计算完成后在UI线程上调用。doInBackground计算的结果作为参数传递给该步骤。

下面通过一个小demo来实现一下:

package com.hxc.supreme.activity;import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;import com.hxc.supreme.R;
import com.hxc.supreme.fragment.QuickControlFragment;import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;/**
* created by huxc on 2017/9/28.
* func:asynctask异步下载
* email: hxc242313@qq.com
*/public class AsyncTaskActivity extends AppCompatActivity implements View.OnClickListener { private Button btnDownload;
private ImageView ivImage;
private TextView tvProgress;
private final String url = "http://i10.hoopchina.com.cn/hupuapp/bbs/741/36556741/thread_4847_36556741_20180619162627_83601.jpg?x-oss-process=image/resize,w_800/format,jpg"; @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_asynctask);
initView();
initListener();
initData();
} protected void initView() {
btnDownload = (Button) findViewById(R.id.btn_download);
ivImage = (ImageView) findViewById(R.id.iv_image);
tvProgress = (TextView) findViewById(R.id.tv_progress);
} protected void initListener() {
btnDownload.setOnClickListener(this);
} protected void initData() { } @Override
public void onClick(View view) {
if (view == btnDownload) {
MyAsyncTask asyncTask = new MyAsyncTask();
asyncTask.execute(url);
}
} class MyAsyncTask extends AsyncTask<String, String, Bitmap> { @Override
protected void onPreExecute() {
super.onPreExecute();
tvProgress.setText("下载开始");
} @Override
protected Bitmap doInBackground(String... strings) {
publishProgress("正在下载中...");
return getBitmapFromUrl(strings[0]);
} @Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
tvProgress.setText("正在下载中...");
} @Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
ivImage.setImageBitmap(bitmap);
tvProgress.setText("下载结束");
}
} public Bitmap getBitmapFromUrl(String urlString) {
Bitmap bitmap;
InputStream is = null; try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
is = new BufferedInputStream(connection.getInputStream());
bitmap = BitmapFactory.decodeStream(is);
connection.disconnect();
return bitmap; } catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}}

布局也很简单就一个button、imageview、TextView

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ImageView
android:id="@+id/iv_image"
android:layout_width="300dp"
android:layout_height="400dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" /> <TextView
android:id="@+id/tv_pro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="当前下载进度:"
app:layout_constraintLeft_toLeftOf="@+id/iv_image"
app:layout_constraintTop_toBottomOf="@+id/iv_image" /> <TextView
android:id="@+id/tv_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
app:layout_constraintLeft_toRightOf="@+id/tv_pro"
app:layout_constraintTop_toTopOf="@+id/tv_pro" /> <Button
android:id="@+id/btn_download"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="20dp"
android:background="@drawable/shape_config_btn_bg"
android:text="开始下载"
app:layout_constraintBottom_toBottomOf="parent" /></android.support.constraint.ConstraintLayout>

最终的效果图就上我们可爱的刘人语小姐姐吧:

Android 消息异步处理之AsyncTask     Android 消息异步处理之AsyncTask

 
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295