Android Service 与多个 Activity 交互

发布于 2024-09-07 15:54:57 字数 569 浏览 1 评论 0原文

我正在尝试重构/重新设计 Android 应用程序。目前,我有一个 UI 活动 (Activity 1),用于创建 DataThread。该线程负责网络 I/O 并通过处理程序与 UI 活动交互(提供数据)。

现在,我想添加另一个活动(带有视频的新 UI 屏幕)- Activity 2活动 1 仍然是主要活动。当用户单击Activity 1 上的按钮时,将调用Activity 2Activity 2的数据也来自DataThread

我的想法是将 DataThread 的逻辑放入 Android Service (DataService) 中。我的问题是 - 是否可以同时将多个活动绑定到我的DataService?有没有办法告诉服务仅向特定活动提供数据?

还有其他想法欢迎吗?

提前致谢。

I'm trying to refactor/redesign an Android app. Currently, I've one UI activity (Activity 1) that creates a DataThread. This thread is responsible for network I/O and interacts (provides data) with the UI activity via a handler.

Now, I want to add another activity (a new UI screen with Video) - Activity 2. Activity 1 is still the main activity. Activity 2 will be invoked when the user clicks a button on Activity 1. Activity 2's data also comes from the DataThread.

My idea is to put the logic of my DataThread inside an Android Service (DataService). My question is - can more than on activity bind to my DataService at the same time? Is there a way to tell the service to provide data to a specific activity only?

Any other ideas are welcome?

Thanks in advance.

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

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

发布评论

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

评论(2

疯狂的代价 2024-09-14 15:54:57

肯定不止一项活动可以绑定到您的服务。对于每个绑定,您都会获得一个 onBind()。然后,您的服务将理想地处理与多个活动交互的逻辑,方法是使用服务中 onBind() 中的 ID 或意图(每个活动的您自己的 ID 作为附加)来识别它们。然后,您可以让服务为绑定到它的每个活动生成一个后台线程。

Definitely more than one activity can bind to your service. You will get an onBind() for each one that binds. Your service would then ideally handle the logic of interacting with multiple activities by identifying them using an ID or the intent (with your own IDs for each activities as extras) from onBind() in your service. You could then have the Service spawn off a background thread for each activity that binded to it.

梦在夏天 2024-09-14 15:54:57

我通常从应用程序类绑定我的服务,并在应用程序中设置某种控制器类(我猜是“中介”……不确定所有这些模式是如何命名的),用于处理服务和任何活动活动之间的通信是。

这将涉及编写您自己的应用程序类并告诉清单使用该类。我在上一个线程中详细介绍了此过程:

比意图更有效地从 Service 更新 UI 的方法?

您可以通过在 onResume 中向 Application 类发送对其自身的引用来跟踪“当前活动”Activity(也在上面的示例中进行了解释) 。这可以通过从公共基类派生您的 Activity 来完成,该基类具有获取 Application 类的方法(从 getApplicationContext 进行转换),并在此基类的 onResume 中将其自身的引用发送到应用程序。然后,您可以按名称向 DataServiceController 注册活动,并且仅当当前活动已向控制器注册以接收消息时才将消息发送到当前活动。

I usually bind my service from the Application class and have some kind of controller class (like a "mediator" I guess...not sure how all these patterns are named) scoped in the application that handles communications between services and whatever the active Activity is.

This would involve writing your own Application class and telling the Manifest to use this one. I went into more detail on this process in a previous thread:

More efficient way of updating UI from Service than intents?

You could keep track of the "currently active" Activity by sending the Application class a reference to itself in onResume (also explained in the example above). This can be accomplished by deriving your Activities from a common base class that has a way of getting your Application class (casting from getApplicationContext), and in this base class' onResume, send a ref of itself to the application. Then, you can register activities by name with your DataServiceController, for example, and send messages to the current Activity only if it's registered with the Controller to receive them.

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