Android的异步服务调用策略 [英] Android asynchronous service calls strategy

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

问题描述

下面的情况:

  1. 在客户端进行远程调用服务(返回void),并提供 一个回调对象
  2. 服务在后台线程上执行一些长期运行的逻辑 然后使用回调对象来触发醚成败 其中(因为这些操纵可视元素)的执行 活动#runOnUiThread块
  1. Client makes remote call to the service (returns void) and provides a callback object
  2. Service executes some long running logic on the background thread and then uses callback object to trigger ether success or failure which (since these manipulate visual elements) execute in Activity#runOnUiThread block

该方案运行正常。现在的问题是 - 我可以使用AsyncTask的制作 code更简洁(如何?),并会在这样做有什么好处 呀?

The scenario runs fine. The question is - can I use AsyncTask to make code less verbose (how?) and would be there any advantages in doing it that way?

或者我应该刚刚摆脱客户端回调产品总数和 执行改装内返回一定的价值远程服务调用 AsyncTask的#doInBackground?

Or should I just get away from client callbacks alltogether and execute remote service calls retrofitted to return some value within AsyncTask#doInBackground?

推荐答案

这是很难说是否的AsyncTask 将会使事情变得更简洁一些,因为我们不知道冗长的当前实现的。

It is difficult to say whether AsyncTask will make things less verbose, since we don't know the verbosity of your current implementation.

对于我来说,的AsyncTask 意味着我不必担心清理自己的线程(例如,张贴到某种杀作业的LinkedBlockingQueue 我的后台线程等待)。它也消除了工作类我用来创建与 LinkedBlockingQueues 使用自定义。而且,它简化了一下做最后的工作回UI线程。

For me, AsyncTask means I don't have to worry about cleaning up threads myself (e.g., post some sort of kill job to a LinkedBlockingQueue my background thread is waiting on). It also eliminates the custom Job classes I used to create for using with LinkedBlockingQueues. And, it simplifies a bit doing final work back on the UI thread.

在你的情况下,远程服务,UI线程的问题是不太重要,因为活动需要处理本身。

In your case, with a remote service, the UI thread issue is less critical, since the activity needs to handle that itself.

我看不出区别就是你的#2和你的最后一段的内容。在这两种情况下,你的服务将调用回调对象,将使用类似 runOnUiThread(),安排的工作必须在UI线程上完成的。

I don't see what the difference is between your #2 and your last paragraph. In both cases, your service will call the callback object, which will use something like runOnUiThread() to arrange for the work to be done on the UI thread.

AFAIK,只有两种方式有一个服务做任何类型的异步工作方式,让客户知道是做这项工作是由广播意图或回调对象。广播意图是方便,但公众(即其他code可以观看他们)。

AFAIK, the only two ways to have a service doing any sort of asynchronous work let the client know that work is done is by a broadcast Intent or a callback object. Broadcast Intents are convenient but public (i.e., other code can watch for them).

我怀疑我可能没有多大帮助这里,但我只是不知道够不够您的方案,以提供更多的细节。

I suspect I probably have not helped much here, but I just don't know enough of your scenario to provide greater detail.

这篇关于Android的异步服务调用策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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