【Android】使用AsyncTask来处理一些简单的需要后台处理的动作
来源:互联网 发布:通联网络支付有限公司 编辑:程序博客网 时间:2024/04/28 10:33
[0]首先让我们看看官网上是怎么解释AsyncTask的:
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params
, Progress
and Result
, and 4 steps, called onPreExecute
, doInBackground
, onProgressUpdate
and onPostExecute
.
简单的来说AsyncTask就是一个简单的用来避免UI阻塞的,运行在background的工具线程类。我们不需要人工来处理这个线程的生命周期,而是按照一定的规则来执行的。
这个Task有3个参数,分别是传入的参数(Params),执行过程中产生的参数(Process),与最后任务结束返回的结果(Result)。如果有不需要的参数,可以用Void来替代。
有4个步骤,分别是:
onPreExecute():用来处理任务执行前需要做的初始化
doInBackground():真正开始在后台执行操作的步骤(这个是必须override的步骤),在这个步骤中可以使用publishProgress(progress……)来提供用于与UI交互显示信息
onProgressUpdate():用来执行过程中即时显示处理进度的函数(获取到doInBackground里面传递过来的参数而进行显示)
onPostExecute();任务执行结束后做的事情
[1]下面是一个简单的AsyncTask范例:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { protected Long doInBackground(URL... urls) { int count = urls.length; long totalSize = 0; for (int i = 0; i < count; i++) { totalSize += Downloader.downloadFile(urls[i]); publishProgress((int) ((i / (float) count) * 100)); } return totalSize; } protected void onProgressUpdate(Integer... progress) { setProgressPercent(progress[0]); } protected void onPostExecute(Long result) { showDialog("Downloaded " + result + " bytes"); } }
要让一个Task开始执行,只需要像下面这样:
new DownloadFilesTask().execute(url1, url2, url3);
[2]那么如何取消一个AsyncTask呢?
A task can be cancelled at any time by invoking cancel(boolean)
. Invoking this method will cause subsequent calls to isCancelled()
to return true. After invoking this method,onCancelled(Object)
, instead of onPostExecute(Object)
will be invoked after doInBackground(Object[])
returns. To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled()
periodically from doInBackground(Object[])
, if possible (inside a loop for instance.)
我们可以在任何时候call cancel(boolean)的方法来取消一个Task,如果呼叫到这个方法会导致之后呼叫isCancelled()
返回true.那么如果这样的话,之后会用onCancelled(Object)
来替代onPostExecute(Object)的执行。
为了确保这个Task能够尽快被取消,我们需要在 doInBackground(Object[])执行的时候去check isCancelled()
- The task instance must be created on the UI thread.(这个task必须在UI thread中创建)
execute(Params...)
must be invoked on the UI thread.(必须在UI thread中叫起执行task)- Do not call
onPreExecute()
,onPostExecute(Result)
,doInBackground(Params...)
,onProgressUpdate(Progress...)
manually.(不要手动去呼叫那4个方法) - The task can be executed only once (an exception will be thrown if a second execution is attempted.) (这个task必须是单次执行的,不要在这个任务没有结束前再次呼叫)
AsyncTask guarantees that all callback calls are synchronized in such a way that the following operations are safe without explicit synchronizations.
- Set member fields in the constructor or
onPreExecute()
, and refer to them indoInBackground(Params...)
. - Set member fields in
doInBackground(Params...)
, and refer to them inonProgressUpdate(Progress...)
andonPostExecute(Result)
.
- 【Android】使用AsyncTask来处理一些简单的需要后台处理的动作
- Android中使用AsyncTask实现简单的异步处理
- 后台需要处理的
- Android--后台处理--AsyncTask类
- Cocos2D-Android- 动作的处理
- Android 系列 4.10使用AsyncTask做后台处理
- 需要处理的一些数据
- android 任务后台处理事物 asyncTask封装
- AsyncTask 使用后台线程处理耗时操作
- Android AsyncTask 的简单使用
- Android中AsyncTask的处理后台耗时操作(如list排序,进度加载)
- Android 使用AsyncTask 后监听异步加载完毕的动作
- Android使用AsyncTask后监听异步加载完毕的动作
- Android 使用AsyncTask 后监听异步加载完毕的动作
- Android 使用AsyncTask 后监听异步加载完毕的动作
- android的事件处理2(AsyncTask)
- Android异步处理:AsyncTask的实现原理
- 安卓异步处理的使用AsyncTask
- YII Framework学习教程-YII的Model-实现原理-2011-11-22
- C#操作ACCESS数据库(创建,压缩,备份,恢复)
- s3c2440硬件学习笔记----嵌入式Linux基础知识和开发环境的构建
- java 中的反射
- Bash Offical Download Site
- 【Android】使用AsyncTask来处理一些简单的需要后台处理的动作
- iBatis动态生成列在执行查询时报列名无效
- 网易游戏2011.10.15校园招聘会笔试题
- 6.2例题--摘花生--2950
- Android 避免内存泄露
- 腾讯2011.10.15校园招聘会笔试题
- 1122
- C++函数中那些不可以被声明为虚函数的函数
- mysql创建表失败 ERROR 1005 (HY000): Can't create table (errno: 13)