编程语言
首页 > 编程语言> > AsyncTask相关知识,android高级应用编程实战

AsyncTask相关知识,android高级应用编程实战

作者:互联网

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

AsyncTask注意事项:


相关视频:

=====

Android必学-AsyncTask基础

相关文章:

=====

深入理解AsyncTask

AsyncTask为何而生?

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

封装、简化异步操作:我们要实现异步任务,通常会使用子线程和线程池,同时,线程结束时我们还要使用handler通知主线程去更新UI,而AsyncTask将这一系列操作都封装起来,方便我们使用。

构建AsyncTask子类的参数

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

AsyncTask<Params,Progress,Result>是一个抽象类,通常用于被继承,继承AsyncTask需要指定如下三个泛型参数

构建AsyncTask子类的回调方法

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

代码编写实例之加载网络图片:

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

Activity:

public class AsyncActivity extends AppCompatActivity {

@BindView(R.id.image_view)

ImageView imageView;

@BindView(R.id.progress_bar)

ProgressBar progressBar;

private static String url =

“http://e.hiphotos.baidu.com/image/pic/item/a1ec08fa513d2697e542494057fbb2fb4316d81e.jpg”;

@Override

protected void onCreate(Bundle savedInstanceState) {

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(Bitmap 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_heigh

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

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

t=“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> {

标签:layout,void,编程,bitmap,AsyncTask,android,id
来源: https://blog.csdn.net/flunsna/article/details/122044132