AsyncTask相关知识,源码解析

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_async);

ButterKnife.bind(this);

initData();

}

private void initData() {

new TestAsyncTask().execute(url);

}

class TestAsyncTask extends AsyncTask<String, Void, Bitmap> {

@Override

protected void onPreExecute() {

super.onPreExecute();

progressBar.setVisibility(View.VISIBLE);

}

@Override

protected void onPostExecute(Bitma

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

p bitmap) {

super.onPostExecute(bitmap);

//操作UI,设置图像

progressBar.setVisibility(View.GONE);

imageView.setImageBitmap(bitmap);

}

@Override

protected Bitmap doInBackground(String… strings) {

//获取传递进来的参数

String url = strings[0];

Bitmap bitmap = null;

URLConnection connection;

InputStream inputStream;

try {

//获取网络连接对象

connection = new URL(url).openConnection();

//获取输入流

inputStream = connection.getInputStream();

BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

Thread.sleep(2000);

//将输入流解析成bitmap

bitmap = BitmapFactory.decodeStream(bufferedInputStream);

//关闭输入流

inputStream.close();

bufferedInputStream.close();

} catch (IOException e) {

e.printStackTrace();

} catch (InterruptedException e) {

e.printStackTrace();

}

return bitmap;

}

}

}

布局:

<?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:layout_width=“match_parent”

android:layout_height=“match_parent”

tools:context=“com.gs.sumok2.asynctask.AsyncActivity”

android:orientation=“vertical”>

<ImageView

android:id="@+id/image_view"

android:layout_width=“200dp”

android:layout_height=“200dp” />

<ProgressBar

android:id="@+id/progress_bar"

android:layout_gravity=“center_horizontal”

android:visibility=“gone”

android:layout_width=“100dp”

android:layout_height=“100dp” />

代码编写实例之模拟水平进度加载条:

=================

Activity:

public class Async2Activity extends AppCompatActivity {

@BindView(R.id.progressBar)

ProgressBar progressBar;

private Test2AsyncTask mTask;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_async2);

ButterKnife.bind(this);

mTask = new Test2AsyncTask();

mTask.execute();

}

@Override

protected void onPause() {

super.onPause();

//不为空而且处于Running状态

if (mTask != null && mTask.getStatus() == AsyncTask.Status.RUNNING) {

//cancel方法只是将对应的AsyncTask标记为cancel状态,并不是真正地取消线程的执行

mTask.cancel(true);

}

}

class Test2AsyncTask extends AsyncTask<Void, Integer, Void> {

@Override

protected Void doInBackground(Void… voids) {

//模拟进度更新

for (int i = 0; i < 100; i++) {

if (isCancelled()){

break;

}

publishProgress(i);

try {

Thread.sleep(300);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

return null;

}

@Override

protected void onProgressUpdate(Integer… values) {

super.onProgressUpdate(values);

if (isCancelled()){

return;

}

//获取进度更新值

progressBar.setProgress(values[0]);

}

}

}

布局:

<?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:layout_width=“match_parent”

android:layout_height=“match_parent”

android:padding=“16dp”

tools:context=“com.gs.sumok2.asynctask.Async2Activity”

android:orientation=“vertical”>

<ProgressBar

android:id="@+id/progressBar"

style="?android:attr/progressBarStyleHorizontal"

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_marginTop=“108dp” />

上一篇:C# 之 4个访问修饰符和8个声明修饰符详解


下一篇:SDTM Model -- Identifier variable