Android异步操作AsyncTask

本文详细介绍了Android中用于异步操作的AsyncTask类,包括其参数类型、执行流程和如何更新进度条。通过一个加载图片的例子,展示了如何在后台执行任务并在UI线程更新结果。
摘要由CSDN通过智能技术生成

转载请标明出处:http://blog.csdn.net/wu_wxc/article/details/53706042
本文出自【吴孝城的CSDN博客】

官网地址:https://developer.android.com/guide/components/processes-and-threads.html
创建继承AsyncTask的类,三个参数
1、Params:执行任务时发送给任务的参数类型
2、Progress:在后台计算时发布的进度单位元的类型
3、Result:返回的结果类型
异步操作的执行:
调用:execute()
先执行:onPreExecute()
然后执行:doInBackground,会有一个返回值
再执行:onPostExecute。doInBackground的返回值在传到这里
要更新进度条,在doInBackground中调用publishProgress()
更新进度条的UI操作在onProgressUpdate中执行

下面以加载一张图片为例

这里写图片描述
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@d
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值