我有点困惑处理器,AsyncTask和线程之间的区别在Android。我在StackOverflow上读过不少博客和问题。
Handler是后台线程,为您提供与UI通信。例如,更新进度条应该通过Handler来完成。使用Handlers可以获得MessagingQueues的优势,因此如果您想调度消息或更新多个UI元素或有重复任务。
AsyncTask是类似的,事实上,他们利用Handler,但不运行在UI线程,所以它很适合抓取数据,例如抓取web服务。稍后您可以与UI交互。
然而,线程不能与UI交互,提供更多的“基本”线程,你错过了AsyncTask的所有抽象。
但是,我希望在服务中运行套接字连接。这应该运行在一个处理程序或线程,甚至一个AsyncTask?UI交互根本不需要。它对我使用的性能有影响吗?
同时,文档也得到了很大的改进。
在我看来,线程并不是进行套接字连接的最有效的方式,但它们确实在运行线程方面提供了最多的功能。我这么说是因为根据经验,长时间运行线程会导致设备非常热且资源密集。即使是一个简单的while(正确)也会在几分钟内使手机发热。如果你说UI交互不重要,也许AsyncTask是好的,因为它们是为长期流程设计的。这只是我的看法。
更新
请忽略我以上的回答!早在2011年,我就回答了这个问题,当时我对Android的经验远不如现在。我上面的回答是误导性的,被认为是错误的。我把它留在那里,因为很多人在下面评论纠正我,我已经吸取了教训。
There are far better other answers on this thread, but I will at least give me more proper answer. There is nothing wrong with using a regular Java Thread; however, you should really be careful about how you implement it because doing it wrong can be very processor intensive (most notable symptom can be your device heating up). AsyncTasks are pretty ideal for most tasks that you want to run in the background (common examples are disk I/O, network calls, and database calls). However, AsyncTasks shouldn't be used for particularly long processes that may need to continue after the user has closed your app or put their device to standby. I would say for most cases, anything that doesn't belong in the UI thread, can be taken care of in an AsyncTask.
在我看来,线程并不是进行套接字连接的最有效的方式,但它们确实在运行线程方面提供了最多的功能。我这么说是因为根据经验,长时间运行线程会导致设备非常热且资源密集。即使是一个简单的while(正确)也会在几分钟内使手机发热。如果你说UI交互不重要,也许AsyncTask是好的,因为它们是为长期流程设计的。这只是我的看法。
更新
请忽略我以上的回答!早在2011年,我就回答了这个问题,当时我对Android的经验远不如现在。我上面的回答是误导性的,被认为是错误的。我把它留在那里,因为很多人在下面评论纠正我,我已经吸取了教训。
There are far better other answers on this thread, but I will at least give me more proper answer. There is nothing wrong with using a regular Java Thread; however, you should really be careful about how you implement it because doing it wrong can be very processor intensive (most notable symptom can be your device heating up). AsyncTasks are pretty ideal for most tasks that you want to run in the background (common examples are disk I/O, network calls, and database calls). However, AsyncTasks shouldn't be used for particularly long processes that may need to continue after the user has closed your app or put their device to standby. I would say for most cases, anything that doesn't belong in the UI thread, can be taken care of in an AsyncTask.
深入观察之后,事情就一目了然了。
AsyncTask:
这是一种使用线程的简单方法,无需了解任何java线程模型。
AsyncTask给出了各个工作线程和主线程的回调。
用于小的等待操作,如下所示:
从web服务中获取一些数据并在布局中显示。
数据库查询。
当你意识到运行的操作永远不会被嵌套。
处理程序:
当我们在android中安装一个应用程序时,它会为该应用程序创建一个名为MAIN UI thread的线程。所有活动都在该线程中运行。根据android单线程模型规则,我们不能访问UI元素(位图,textview等)直接为另一个线程内定义的活动。
Handler允许你从其他后台线程与UI线程通信。这在android中很有用,因为android不允许其他线程直接与UI线程通信。处理程序可以发送和处理与线程的MessageQueue关联的Message和Runnable对象。每个Handler实例都与单个线程和该线程的消息队列相关联。创建新的Handler时,它将绑定到创建它的线程的线程/消息队列。
它最适合:
它允许您进行消息排队。
消息调度。
线程:
现在是讨论线程的时候了。
线程是AsyncTask和Handler的父线程。它们都在内部使用线程,这意味着您也可以创建自己的线程模型,如AsyncTask和Handler,但这需要对Java的多线程实现有很好的了解。
public class RequestHandler {
public String sendPostRequest(String requestURL,
HashMap<String, String> postDataParams) {
URL url;
StringBuilder sb = new StringBuilder();
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
sb = new StringBuilder();
String response;
while ((response = br.readLine()) != null){
sb.append(response);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
}