Android 中的 AsyncCallBack 是什么?
谁能给我一个清晰的描述什么是AsyncCallBack?最新的android版本中是否存在该功能?因为当我搜索 AsyncCallBack 时,我被定向到 AsyncTask...这两者相同吗?有人有例子吗?
提前致谢。
Can anyone give me a clear description of what is AsyncCallBack? Does it exist in the latest android version? Because I am directed to AsyncTask when I search for AsyncCallBack.... Are these both same? Does anyone have an example?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
android 中没有名为 AsyncCallback 的类。我认为您正在寻找的是 AsyncTask,它是一种在另一个线程上运行一段代码的方法,这样 UI 就不会被阻塞,并在 UI 线程上接收它的结果。例如,假设您想与互联网上的服务器通信以响应用户单击 UI 中的某些内容,然后从服务器接收一些结果并更新 UI。与执行常规线程代码相比,AsyncTask 使执行此操作变得非常容易,因为线程生命周期和返回 UI 线程的通信已为您处理。作为奖励,还支持取消后台任务,但您必须编写代码来在调用取消时处理它。如果您不做一些工作,它就无法做到这一点。
There is no class named AsyncCallback in android. I think what you are looking for is AsyncTask which is a way to run a piece of code on another Thread so the UI won't be blocked, and receive it's results on the UI thread. For example, say you want to talk to a server on the internet in response to the user clicking something in the UI, then receive some result from the server, and update the UI. AsyncTask makes doing this very easy compared to doing regular threading code because threading lifecycle and communication back to the UI thread is handled for you. As a bonus there is also support for canceling a background task, but you have to write the code to handle it when cancel is called. It doesn't do it without some work on your side.
我认为这些术语可能在这里混淆了,Android 中没有
AsyncCallback
(据我所知)。不过,GWT (Google Web Toolkit) 中有一个使用非常广泛的AsyncCallback
接口:http://google-web-toolkit.googlecode.com/svn/javadoc/2.1/com/google/gwt/user/client/ rpc/AsyncCallback.html。在 Android 中,您可以使用
AsyncTask
在主 UI/应用线程的单独线程上轻松异步运行后台操作:http://developer.android.com/reference/android/os/AsyncTask.html。这是关于
AsyncTask
的一篇很好的介绍文章:http:// /developer.android.com/resources/articles/painless-threading.html。I think the terms may be mixed up here, there isn't an
AsyncCallback
in Android (as far as I know). There is however very widely usedAsyncCallback
interface in GWT (Google Web Toolkit): http://google-web-toolkit.googlecode.com/svn/javadoc/2.1/com/google/gwt/user/client/rpc/AsyncCallback.html.In Android you use
AsyncTask
to easily run background operations asynchronously on a separate Thread from the main UI/app thread: http://developer.android.com/reference/android/os/AsyncTask.html.Here is a good intro article on
AsyncTask
: http://developer.android.com/resources/articles/painless-threading.html.