AsyncTask 相关知识,安卓工程师面试题及答案
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 T
estAsyncTask().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_height="200dp" />
<ProgressBar
android:id="@+id/progress_bar"
android:layout_gravity="center_horizontal"
android:visibility="gone"
android:layout_width="100dp"
android:layout_height="100dp" />
</LinearLayout>
代码编写实例之模拟水平进度加载条:
=================
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;
}
评论