转载请标明出处: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