AsyncTask中的4个回调

onPreExecute(),在doInBackground(Params...)之前运行在UI线程中

onPostExecute(Result),在doInBackground(Params...)之后运行在UI线程中

onProgressUpdate(Progress...),在publishProgress(Progress...)被调用后运行在UI线程中

doInBackground(Params...),运行在子线程中,这个函数可以调用publishProgress以发布更新到UI线程

示例如下

    public class Md5AsyncTask extends AsyncTask<String, Long, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.d(TAG, "onPreExecute() called");
tv1.setText("已执行");
} @Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d(TAG, "onPostExecute(String) called");
tv3.setText("已结束");
tvResult.setText(s);
} @Override
protected void onProgressUpdate(Long... values) {
super.onProgressUpdate(values);
tv2.setText(values[0] + "%");
} @Override
protected String doInBackground(String... strings) {
File file = new File(strings[0]);
long fileSize = file.length();
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[10 * 1024 * 1024];
int numberOfBytesRead;
long totalNumberOfBytesRead = 0;
while ((numberOfBytesRead = bis.read(buffer)) != -1) {
md5.update(buffer, 0, numberOfBytesRead);
totalNumberOfBytesRead += numberOfBytesRead;
publishProgress(totalNumberOfBytesRead * 100 / fileSize);
}
StringBuilder result = new StringBuilder();
byte[] digested = md5.digest();
for (byte e : digested) {
String hexStr = Integer.toHexString(e & 255);
if (hexStr.length() == 1)
result.append('0');
result.append(hexStr);
}
return result.toString();
} catch (IOException | NoSuchAlgorithmException ex) {
Log.e(TAG, null, ex);
return null;
}
}
}
new Md5AsyncTask().execute(filename);

Android笔记之AsyncTask的更多相关文章

  1. Android应用开发学习笔记之AsyncTask

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 在上一篇文章中我们学习了多线程和Handler消息处理机制,如果有计算量比较大的任务,可以创建一个新线程执行计算工作 ...

  2. Android笔记(三十六) AsyncTask是如何执行的?

    在上一个例子中,我们是在LoadImage的onPostExecute中修改的UI,不是说只允许在主线程中修改UI吗?我们看一下源代码是如何操作的. MainActicity.java package ...

  3. [转]【安卓笔记】AsyncTask源码剖析

    [转][安卓笔记]AsyncTask源码剖析 http://blog.csdn.net/chdjj/article/details/39122547 前言: 初学AsyncTask时,就想研究下它的实 ...

  4. Android笔记——Android中数据的存储方式(二)

    我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...

  5. Android笔记:触摸事件的分析与总结----TouchEvent处理机制

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://glblong.blog.51cto.com/3058613/1559320   ...

  6. Android开发之AsyncTask的使用

    Android API 3时引进了AsyncTask,也叫异步任务.使用它可以很方便的更新主线程中的UI,使用它比Handler.Thread更简单.由于AsyncTask是抽象类,要使用它首先要创建 ...

  7. Android 笔记之 R 文件

    Android笔记之R文件 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: red; te ...

  8. Android 笔记之 Android 系统架构

    Android笔记之Android系统架构 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: ...

  9. Android笔记之使用Glide加载网络图片、下载图片

    Glide简介 不想说太多,真的很方便:P)可以节省我不少时间 GitHub地址:https://github.com/bumptech/glide 加载网络图片到ImageView Glide.wi ...

随机推荐

  1. zoj 3882 Help Bob(zoj 2015年7月月赛)

    Help Bob Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a game very popular in ZJU at pr ...

  2. Solr 配置文件之schema.xml

    schema.xml这个配置文件的根本目的是为了通过配置告诉Solr怎样建立索引. solr的数据结构例如以下: document:一个文档.一条记录 field:域.属性 solr通过搜索某个或某些 ...

  3. Windows / Linux 一件编译zlib库

    一. 下载zlib库 : http://www.zlib.net 本文以  zlib-.tar.xz  为例 二. 解压文件得到 zlib- 文件夹,修改 zlib-/CMakeLists.txt 文 ...

  4. AnimatorStateInfo

    AnimatorStateInfo Namespace: UnityEngine   Description Information about the current or next state. ...

  5. Linux非阻塞IO(五)使用poll实现非阻塞的回射服务器客户端

    前面几节我们讨论了非阻塞IO的基本概念.Buffer的设计以及非阻塞connect的实现,现在我们使用它们来完成客户端的编写. 我们在http://www.cnblogs.com/inevermore ...

  6. Java学习从入门到精通(1) [转载]

    Java Learning Path (一).工具篇 一. JDK (Java Development Kit) JDK是整个Java的核心,包括了Java运行环境(Java Runtime Envi ...

  7. 跨discuz站获取

    1.在需要取得formhash的页面加入下面js代码,还需要jquery库. <script lanuage="javascript"> $(function(){ $ ...

  8. CentOS7网络桥接模式下配置-经典完备

    原文地址:http://blog.csdn.net/youzhouliu/article/details/51175364 首先要将Vmware设置为桥接模式: 并选择宿主机连接的网路进行桥接: Ce ...

  9. Docker在Centos下使用Dockerfile构建远程Tomcat和Jenkins镜像

    镜像构建准备环境原料 构建CentOS Docker tomcat镜像 Dockerfile文件内容: FROM centos:latest MAINTAINER boonya <boonya@ ...

  10. list集合转换成json类型

    public String gettext(HttpServletRequest request,HttpServletResponse response){ List<xuanhuan_> ...