Android的,帖子 - 处理器/ AsyncTask的 [英] Android, Threads - handlers/asynctask

查看:108
本文介绍了Android的,帖子 - 处理器/ AsyncTask的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是一个小白通过书和线程我看到有一些在书中解释的3个主要途径学习Android的:

Am a noob learning Android via a book and for threading I see there are 3 main ways that are explained in the book:


  • 通过信息处理程序

  • 通过后处理程序

  • 的AsyncTask

3的所有看起来不错,我的问题是:结果
是否有当您使用上述任何一个具体的时间?或者是个人的preference?

All 3 seem good, my question is:
Is there a specific time when you use one of the above? Or is it personal preference?

编辑:结果
如果它不是个人的preference,请给我一个例子是,当你将使用一个,如果没有其他的(甚至是链接将AP preciated)


If its not personal preference, please give me an example as to when you would use one if not the other (even a link would be appreciated)

由于我上有运行多个线程规划(例如,一个总的应用程序运行时间,一个答案之前,时间选择等的应用,一个是移动的背景图像等),并想知道哪些选择。我能做什么,我想使用这两种方法3所以很困惑,为什么有3时,它可以与任何一个来完成的功能。

Because I am planning on having multiple threads running (eg, one for total app running time, one for time before answer is selected etc in the app, one for moving the background image etc) and want to know which to choose. I can do the functionality of what I want using either of these 3 methods so am confused as to why there are 3 when it can be done with either one.

谢谢!

推荐答案

处理程序是允许你执行一些code在不同的线程,通常UI线程一个非常基本的方式。它不会告诉你如何运行你的线程,你可以自由地做任何你在这方面想。如果你有明确的消息,我会选择的handleMessage 办法。这就是你需要有发送和接收的一些数据。如果你只是需要做的事情,你可以用的Runnable 。然而,这两种方法都可以使用和选择往往会是preference的结果。

Handler is a very basic way that allows you to execute some code on a different thread, typically UI thread. It does not tell you how to run your threads and you are free to do whatever you want in that respect. I would choose handleMessage approach if you have explicit messaging. That is you need to have some data sent and received. If you just need to do something, you can use Runnable. However, both methods are usable and a choice often would be result of a preference.

的AsyncTask 是使用引擎盖下的处理程序更高层次的概念。如果你使用它,你将不必处理线程自己。您的异步code将通过线程池来执行,该框架控制。您将有两种方法,使您可以执行的UI线程code( onPostExecute onProgressUpdate

AsyncTask is higher level concept that uses handlers under the hood. If you use it, you will not have to deal with threads yourself. Your asynchronous code will be executed via thread pool and controlled by the framework. You will have two methods that allow you to execute code on UI thread (onPostExecute and onProgressUpdate).

在简单地说,选择的AsyncTask 为您多线程办最多的事,将免除你的需要处理线程管理自己。如果你必须有你自己的线程才使用的处理程序。但是请注意,还有其他的情况下,当你必须使用处理程序。例如,使者。此外,我在使用 onProgressUpdate 不充分的情况下过。在这种情况下,我会用处理器以及从 doInBackground

In a nutshell, choose AsyncTask for most things that you do with multiple threads it will absolve you of a need to deal with thread management yourself. If you must have your own threads only then use handlers. Note however, that there are other cases when you have to use handlers. For example, with Messenger. Also I had cases when using onProgressUpdate is not sufficient. In this case I would use handler as well from doInBackground.

这篇关于Android的,帖子 - 处理器/ AsyncTask的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆