AsyncTaskUtilsActivity
来源:互联网 发布:手机淘宝如何快速秒杀 编辑:程序博客网 时间:2024/06/06 21:11
AsyncTaskUtilsActivity
private ImageView mImgIv;
private ProgressBar mProgressBar;
private TextView mProgressTv;
private LinearLayout mProgressLayout;
private Bitmap mBitmap;
private String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502265730325&di=6bb9375cc434eee93687156f467c2092&imgtype=0&src=http%3A%2F%2Fcdn.duitang.com%2Fuploads%2Fitem%2F201505%2F14%2F20150514001829_Fwa8X.jpeg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_net);
initView();
//执行请求任务,传入请求url
new MyAsyncTask().excute(url);
}
/**
* 初始化控件
*/
private void initView() {
mImgIv = (ImageView) findViewById(R.id.iv_img);
mProgressBar = (ProgressBar) findViewById(R.id.pb_progress);
mProgressTv = (TextView) findViewById(R.id.tv_progress);
mProgressLayout = (LinearLayout) findViewById(R.id.layout_progress);
}
/**
* 创建MyAsyncTask类,实现AsyncTaskUtils抽象类的几个抽象方法
*/
class MyAsyncTask extends AsyncTaskUtils<String, Integer, Bitmap> {
/**
* 网络请求之后的逻辑处理(处理请求之后的ui刷新工作)运行在主线程
* @param bitmap
*/
@Override
protected void postExecute(Bitmap bitmap) {
mProgressLayout.setVisibility(View.GONE);
mImgIv.setImageBitmap(bitmap);
}
/**
* 下载过程的方法,开启子线程下载数据(运行在子线程)
* @param S 请求地址
* @return
*/
@Override
protected Bitmap doInBackground(String S) {
try {
URL url = new URL(S);
HttpURLConnection conection = (HttpURLConnection) url.openConnection();
conection.connect();
int progress = 0;
int currProgress = 0;
//拿到返回数据的总长度
int totalLength = conection.getContentLength();
Message msg = new Message();
if (200 == conection.getResponseCode()) {
InputStream in = conection.getInputStream();
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
int length = -1;
byte[] buffer = new byte[1];
while ((length = in.read(buffer)) != -1) {
currProgress += length;
progress = (int) ((float) currProgress / totalLength * 100);
final int finalProgress = progress;
runOnUiThread(new Runnable() {
@Override
public void run() {
onProgressUpdate(finalProgress);
}
});
byteOut.write(buffer, 0, length);
}
return BitmapFactory.decodeByteArray(byteOut.toByteArray(), 0, byteOut.size());
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 网络请求之前的准备工作(主要处理一些网络请求之前的初始化操作,比如弹出一个加载dialog等)
*/
@Override
protected void onPreExecute() {
mProgressLayout.setVisibility(View.VISIBLE);
}
/**
* 更新进度条
* @param progress 当前进度
*/
@Override
protected void onProgressUpdate(int progress) {
mProgressBar.setProgress(progress);
mProgressTv.setText(progress+"%");
}
}
private ProgressBar mProgressBar;
private TextView mProgressTv;
private LinearLayout mProgressLayout;
private Bitmap mBitmap;
private String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502265730325&di=6bb9375cc434eee93687156f467c2092&imgtype=0&src=http%3A%2F%2Fcdn.duitang.com%2Fuploads%2Fitem%2F201505%2F14%2F20150514001829_Fwa8X.jpeg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_net);
initView();
//执行请求任务,传入请求url
new MyAsyncTask().excute(url);
}
/**
* 初始化控件
*/
private void initView() {
mImgIv = (ImageView) findViewById(R.id.iv_img);
mProgressBar = (ProgressBar) findViewById(R.id.pb_progress);
mProgressTv = (TextView) findViewById(R.id.tv_progress);
mProgressLayout = (LinearLayout) findViewById(R.id.layout_progress);
}
/**
* 创建MyAsyncTask类,实现AsyncTaskUtils抽象类的几个抽象方法
*/
class MyAsyncTask extends AsyncTaskUtils<String, Integer, Bitmap> {
/**
* 网络请求之后的逻辑处理(处理请求之后的ui刷新工作)运行在主线程
* @param bitmap
*/
@Override
protected void postExecute(Bitmap bitmap) {
mProgressLayout.setVisibility(View.GONE);
mImgIv.setImageBitmap(bitmap);
}
/**
* 下载过程的方法,开启子线程下载数据(运行在子线程)
* @param S 请求地址
* @return
*/
@Override
protected Bitmap doInBackground(String S) {
try {
URL url = new URL(S);
HttpURLConnection conection = (HttpURLConnection) url.openConnection();
conection.connect();
int progress = 0;
int currProgress = 0;
//拿到返回数据的总长度
int totalLength = conection.getContentLength();
Message msg = new Message();
if (200 == conection.getResponseCode()) {
InputStream in = conection.getInputStream();
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
int length = -1;
byte[] buffer = new byte[1];
while ((length = in.read(buffer)) != -1) {
currProgress += length;
progress = (int) ((float) currProgress / totalLength * 100);
final int finalProgress = progress;
runOnUiThread(new Runnable() {
@Override
public void run() {
onProgressUpdate(finalProgress);
}
});
byteOut.write(buffer, 0, length);
}
return BitmapFactory.decodeByteArray(byteOut.toByteArray(), 0, byteOut.size());
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 网络请求之前的准备工作(主要处理一些网络请求之前的初始化操作,比如弹出一个加载dialog等)
*/
@Override
protected void onPreExecute() {
mProgressLayout.setVisibility(View.VISIBLE);
}
/**
* 更新进度条
* @param progress 当前进度
*/
@Override
protected void onProgressUpdate(int progress) {
mProgressBar.setProgress(progress);
mProgressTv.setText(progress+"%");
}
}
阅读全文
0 0
- AsyncTaskUtilsActivity
- FreeRTOS----任务优先级
- wordBreak-ii
- 常量对象、常量成员函数及其重载
- 移动零
- Spring集成RabbitMQ-使用RabbitMQ更方便
- AsyncTaskUtilsActivity
- Dialog System Using Real-Time Crowdsourcing and Twitter Large-Scale Corpus
- WARNING: terminal is not fully functional
- hihocoder:#1520 : 古老数字
- Maven的下载和环境配置
- 计算几何之二分半平面交
- Android Studio中CodeStyle模板的配置
- 学习记录--颤抖吧,hadoop(五)---搭建完全分布式hadoop集群(1)
- 人工智能引发的四大场景革命