处理程序VS异步调用Android中

处理程序VS异步调用Android中

本文介绍了处理程序VS异步调用Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我使用的处理程序来调用Web服务方法,使其在后台运行。现在的问题是它的花更多的时间,得到的反应,这似乎是在性能方面更加昂贵。现在我打算去的异步调用,这将是最好的?什么是处理程序和异步Android中调用之间的差异?请帮我想出了一个最佳的解决方案。

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.

有关参考我在这里提供一些code片段

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);

当点击登录按钮,我正在调用该方法,它看起来像用户界面调用方法之前绞死几分钟。在该方法中,二昂贵web服务调用是涉及验证和注册的用户。我怎么能正常化的应用程序的缓慢。帮帮我吧。

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.

推荐答案

有一定的优势,使用的和的分别使用的这真的取决于你的使用和这些好处VS利弊剖析将有可能回落到你。

There are certain advantages to using Thread and Handler respectively to using AsyncTask it really depends on your usage and the profiling of those benefits vs detriments will likely come down to you.

我会推荐的文章一小线程在Android上的理解。

I would recommend the article Painless Threading for a little understanding of threading on Android.

修改在问题的更多信息。

如果我们适应从无痛线程被链接你可以得到的东西,像这样的文章。

If we adapt the code from the Painless Threading article that was linked you can get something like so.

  new Thread(new Runnable() {
    public void run() {
      signIn();
      signinHandler.post(new Runnable() {
        public void run() {
          //TODO: Something to notify of login complete / continue processing.
        }
      });
    }
  }).start();

在您需要继续或通知的执行,我不知道什么是处理当前签到()的TODO 因此,如果跨越UI线程就会产生要进行重构。

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.

这篇关于处理程序VS异步调用Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!