Android 中的处理程序与异步调用
目前我正在使用处理程序来调用 Web 服务方法以使其在后台运行。问题是它需要更多的时间来给出响应,在性能方面似乎更昂贵。现在我计划使用异步调用,哪一个是最好的? Android 中的处理程序和异步调用有什么区别?请帮我想出一个最好的解决方案。
为了供您参考,我在这里给出了一些代码片段
signIn.setBackgroundResource(R.drawable.signin_press);
changeImage=new Runnable(){
public void run(){
signIn();
}
};
signinHandler.post(changeImage);
当单击“登录”按钮时,我调用此方法,看起来 UI 在调用该方法之前挂起了几分钟。在此方法中,涉及两个昂贵的 Web 服务调用来验证和注册用户。我如何使应用程序的缓慢正常化。帮我。
Currently i am using Handlers to call web service methods to make it run in background. The problem is its taking more time to give the response, it seems to be more expensive in terms of performance. Now i plan to go for the Async Calls, which will be the best one? What are differences between Handlers and Async Calls in Android? Please help me to come up with a best solution.
For your reference I am giving some code snippets here
signIn.setBackgroundResource(R.drawable.signin_press);
changeImage=new Runnable(){
public void run(){
signIn();
}
};
signinHandler.post(changeImage);
When clicking the Sign in button i am calling this method, it looks like the UI is hanged for few minutes before calling the method. In this method, two expensive web services calls are involved to authenticate and register the user. How i can normalize the slowness of the app. Help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
Thread
有一定的优势Handler
分别使用AsyncTask
这实际上取决于您的使用情况和这些的分析好处与坏处可能会取决于你。我会推荐这篇文章 Painless Threading 来对线程有一点了解在安卓上。
编辑以获取相关的其他信息。
如果我们改编链接的Painless Threading文章中的代码你可以得到类似的东西。
在 TODO 中,您需要继续或通知执行,我不知道
signIn()
当前处理什么,因此如果跨越 UI 线程,则必须重构。There are certain advantages to using
Thread
andHandler
respectively to usingAsyncTask
it really depends on your usage and the profiling of those benefits vs detriments will likely come down to you.I would recommend the article Painless Threading for a little understanding of threading on Android.
EDIT for additional info in question.
If we adapt the code from the Painless Threading article that was linked you can get something like so.
In the TODO you need to continue or notify execution, I don't know what is currently handled in
signIn()
so if that crosses the UI thread it will have to be refactored.AsyncTask 在内部使用线程池和处理程序。这不是魔法;而是魔法。 查看来源。性能不会明显优于您自己的处理程序(除了使用线程池可能会节省创建新线程的一小部分开销,但与典型 Web 服务调用的持续时间相比,这几乎可以忽略不计;额外的开销几毫秒肯定不会对用户产生明显的影响)。考虑到发出 Web 请求涉及的因素有很多,什么会让您认为线程/处理程序是减慢您的应用程序速度的原因(而不是您的网络连接、服务器流量等)?对您的代码进行分析是否支持该断言?
AsyncTask uses a thread pool and handlers internally. It's not magic; see the source. Performance won't be measurably better than your own handlers (except for the fact that using a thread pool may save a small bit of overhead for creating a new thread, but that's pretty negligible compared the the duration of typical web service calls; the extra few milliseconds certainly won't make a user-noticeable impact). Given the number of factors involved in making a web request, what would make you think the thread/handlers are what's slowing down your app (as opposed to your network connection, server traffic, etc.)? Does profiling your code back that assertion up?