asynctask java_java – 如何使用AsyncTask
常见问题和AsyncTask使用的一般说明
=> Where should I do network operations? Where should I return my aquired values?
通常,您应该在单独的线程中执行网络操作 – > doInBackground();因为您不希望在网络操作占用时间时冻结UI.因此,您应该连接到Service或.php脚本,或者从doInBackground()方法中获取数据的任何位置.然后你也可以在那里解析数据,并通过指定doInBackground()的返回类型来回复doInBackground()方法中的解析数据,更多关于那里的数据.然后,onPostExecute()方法将从doInBackground()接收返回的值,并使用UI表示它们.
=> AsyncTask< String, Integer, Long> ==> How does this work?
一般来说,AsyncTask类看起来像这样,它只不过是一个具有3个不同generic types的泛型类:
AsyncTask
You can specify the type of Parameter the AsyncTask takes, the Type of the Progress indicator and the type of the result (the return type
of doInBackGround()).
下面是一个AsyncTask的示例,如下所示:
AsyncTask
我们有参数的类型字符串,进度的类型整数和结果的类型长(返回类型为doInBackground()).您可以使用Params,Progress和Result所需的任何类型.
private class DownloadFilesTask extends AsyncTask {
// these Strings / or String are / is the parameters of the task, that can be handed over via the excecute(params) method of AsyncTask
protected Long doInBackground(String... params) {
String param1 = params[0];
String param2 = params[1];
// and so on...
// do something with the parameters...
// be careful, this can easily result in a ArrayIndexOutOfBounds exception
// if you try to access more parameters than you handed over
long someLong;
int someInt;
// do something here with params
// the params could for example contain an url and you could download stuff using this url here
// the Integer variable is used for progress
publishProgress(someInt);
// once the data is downloaded (for example JSON data)
// parse the data and return it to the onPostExecute() method
// in this example the return data is simply a long value
// this could also be a list of your custom-objects, ...
return someLong;
}
// this is called whenever you call puhlishProgress(Integer), for example when updating a progressbar when downloading stuff
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
// the onPostexecute method receives the return type of doInBackGround()
protected void onPostExecute(Long result) {
// do something with the result, for example display the received Data in a ListView
// in this case, "result" would contain the "someLong" variable returned by doInBackground();
}
}
=> How to use AsyncTask? How can I “call” it? How can I “execute” it?
在这种情况下,AsyncTask将String或String Array作为参数,一旦调用AsyncTask,它将如下所示:(指定的参数用于AsyncTask的execute(param)方法).
new DownloadFilesTask().execute("Somestring"); // some String as param
请注意,此调用没有返回值,您应该使用的唯一返回值是从doInBackground()返回的值.使用onPostExecute()方法确实可以使用返回的值.
这一行代码也要小心:(这个执行实际上会有一个返回值)
long myLong = new DownloadFilesTask().execute("somestring").get();
当AsyncTask正在执行时,.get()调用会导致UI线程被阻塞(因此,如果操作花费的时间超过几毫秒,UI会冻结),因为执行不会在单独的线程中执行.如果删除对.get()的调用,它将异步执行.
=> What does this notation “execute(String… params)” mean?
这是一个带有所谓“varargs”(变量参数)参数的方法.为了简单起见,我只想说这意味着你没有指定你可以通过这个参数传递给方法的实际值的数量,并且你给方法的任何数量的值都将被视为一个数组内的数组.方法.所以这个调用可能是这样的:
execute("param1");
但它也可能看起来像这样:
execute("param1", "param2");
甚至更多的参数.假设我们还在讨论AsyncTask,可以在doInBackground(String … params)方法中以这种方式访问参数:
protected Long doInBackground(String... params) {
String str1 = params[0];
String str2 = params[1]; // be careful here, you can easily get an ArrayOutOfBoundsException
// do other stuff
}
asynctask java_java – 如何使用AsyncTask相关推荐
- java asynctask完成_使用 AsyncTask实现异步处理
概述: AsyncTask是在Android SDK 1.5之后推出的一个方便编写后台线程与UI线程交互的辅助类.AsyncTask的内部实现是一个线程池,每个后台任务会提交到线程池中的线程执行,然 ...
- 【AsyncTask整理 1】 AsyncTask几点要注意的地方
问题1:AsyncTask是多线程吗? 答:是. 问题2:AsyncTask与Handler相比,谁更轻量级? 答:通过看源码,发现AsyncTask实际上就是一个线程池,而网上的说法是AsyncTa ...
- 异步AsyncTask,怎样停止AsyncTask和Thread
我们要知道在java的线程中,没有办法停止一个正在运行中的线程.在Android的AsyncTask中也是一样的.如果必须要停止一个线程,我们可以采用这个线程中设置一个标志位,然后在线程run方法或A ...
- android asynctask 参数,Android中AsyncTask详解
定义 AsyncTask是一个抽象类,在使用时需要继承该类,实现其抽象方法protected abstract Result doInBackground(Params... params).其主要作 ...
- 【Android】AsyncTask异步类
一.关于AysncTask AsyncTask使得多线程编程更加简单,AsyncTask能在后台线程执行异步任务,并且在UI线程更新界面,而不再需要用户去操作Thread和Handler.AysncT ...
- android一个简单的异步AsyncTask下载数示例,简单下载(07)
2019独角兽企业重金招聘Python工程师标准>>> public class MainActivity extends Activity {private ImageView i ...
- Android中 AsyncTask
Android AsyncTask 在程序处理中必然会遇上耗时的操作,如访问网络,下载数据,访问数据库等,如何存在耗时的操作 又不能影响界面显示交互. 在某些耗时可以控制的情况下,我们可以分批操作,对 ...
- AsyncTask工作机制简介
昨天写的图片的三级缓存,假设有兴趣,能够去看下,浅谈图片载入的三级缓存原理(一) http://blog.csdn.net/wuyinlei/article/details/50606455 在里面我 ...
- 异步任务-AsyncTask
异步任务-AsyncTask 为什么要异步任务 android单线程模型 耗时操作放在非主线程中执行 AsyncTask为何而生 子线程更新UI 封装,简化异步操作 异步任务AsyncTask 构件A ...
最新文章
- 关于acm的新手一些问题
- 入行时间序列预测必读的4篇论文(附代码)
- 概念炒作的背后,“智能合约”的真相是什么?
- Eureka 服务注册与发现01——单机版
- C++ :跳表数据结构的实现原理
- redis专题:redis的常用数据结构及使用场景
- 如何看懂wsdl文件
- 8.2捷联惯导算法仿真 代码整理分析(一)
- 堆排序算法——C/C++
- 一篇联想员工写的:联想不是我的家
- speedoffice怎样在word文档中画横线
- 计算机专业创新点子借鉴,[在计算机教学中应如何开展创新教育]大学生创新创业点子300...
- SASE:基于数字身份标识的网络与安全访问云服务
- html+css+js实现的前端模板
- 他是国家的儿子 如不再优秀请原谅他
- Unity Accelerator本地服务器加速Unity项目资源载入速度
- 计算机网络毕业论文搭建服务,网络搭建毕业论文
- echarts自定义legend样式
- python 公众号引流_公众号引流方法有哪些?
- java+mysql 基于ssm的主题酒店预定入住管理系统
热门文章
- 被严重低估的PMO,到底是什么?
- 网络分析法(Analytic Network Process,ANP)
- 10年后端开发程序员精心整理「C/C++ Linux服务器」 成长路线(附思维导图)
- 小黑夜晚冒雨加餐leetcode之旅:2. 两数相加
- 《生产精细化管理与现场改善》
- 安卓工控主板通信接口有哪些呢?
- 关于PLC与编码器的接线问题
- 取消win10桌面防火墙图标的步骤:
- 微信小程序登录流程php,微信小程序登录流程​
- 试述你对计算机软件的认识议论文,计算机软件浅议论文