从活动中调用冗长的服务方法 - 最佳实践

发布于 2024-12-15 06:57:03 字数 561 浏览 0 评论 0原文

我正在开发一个应用程序,其中包含将呼叫转发到网络服务的服务,以及一些发出这些呼叫的活动。这些活动需要处理这些调用的结果。例如,我在服务上有一个 writeComment 方法,该方法访问 Web 服务并返回有关新编写的评论的一些信息。

现在我让 Activity 处理所有线程。 Activity 绑定服务,然后使用 AsyncTask 调用绑定服务的 writeComment 方法。

只要 AsyncTask 运行时 Activity 不停止,一切都会正常进行。如果确实如此(翻转手机时很容易发生),则 AsyncTask 在尝试在 onPostExecute 中更新 UI 时会剧烈死亡。我不完全确定如何解决此问题 - 我确实需要让用户知道服务器已更新。

如果我采取相反的方式,并向服务注册回调,我仍然有点困难,因为我需要通知服务活动已更改 - 我需要告诉它不要在第一个活动的 onDestory 中通知我,并在第二个Activity的onCreate中重新注册。我需要处理异步任务在 onDestroy 之后和 onCreate 之前完成的情况。

在这种情况下什么被认为是最佳实践?

谢谢, 伊泰。

I'm developing an app with a service that forwards calls to a web-service, and a few activities that place those calls. The activities need to process the results of those calls. For example, I have a writeComment method on the service, that accesses the web-service and returns some information about the newly written comment.

Right now I let the Activity take care of all the threading. The Activity binds the service, and then uses an AsyncTask that calls the bound service's writeComment method.

All works well as long as the Activity isn't stopped while the AsyncTask is running. If it does (easily happens when flipping the phone), the AsyncTask dies a violent death when trying to update the UI in onPostExecute. I'm not entirely sure how to fix this - I do need to let the user know the server has been updated.

If I go the other way around, and register a callback with the Service, I'm still a bit stump, because I need to notify the Service the Activity has changed - I need to tell it not to notify me in the first Activity's onDestory, and reregister in the second Activity's onCreate. And I need to handle the case where the asynchronous task completes after onDestroy and before onCreate.

What is considered Best Practice in this case?

Thanks,
Itay.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌上芳菲 2024-12-22 06:57:03

我的直觉告诉我让服务处理线程。与活动相比,服务的瞬态性要小得多(尽管在某种程度上仍然是瞬态的),因此,尝试与不再存在的上下文(无论是活动还是服务)进行交互的线程问题会更少。您看过 IntentService 类吗?它为您处理大量线程。

My intuition tells me to let the service handle the threading. Services are far less transient (although still transient to some degree) than activities and therefore you'll have less issues of threads trying to interact with a Context (be it an Activity or a Service) that's no longer there. Have you looked at the IntentService class? It handles a lot of the threading for you.

安穩 2024-12-22 06:57:03

在我的应用程序中,我有一个长期运行的服务和需要在服务中呈现数据的活动。当发生更改时,服务还会对活动执行 ping 操作,但活动也可以查询服务。我处理这个问题的方法有两个。

首先,我将活动绑定到服务,以便将消息从活动发送到服务。

其次,服务通过广播发送通知,而活动则侦听这些广播。我在 Activity onResume 中设置它,并在 onPause 中将其拆除。我认为这是你所缺少的部分。

In my app, I have a long-running service and Activities that need to render data in the service. The service also pings the Activities when there is a change but the Activity can also query the service. The way I approached this was two-fold.

Firstly, I bind my activity to the Service in order to send messages from Activity to service.

Secondly, the Service sends notifications with Broadcasts and the Activity listens for those broadcasts. I set that up in the Activity onResume and tear it down in the onPause. I think this is the part that you're missing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文