简体   繁体   English

活动之间共享数据的服务

[英]service for sharing data between activities

In the android project that I have, all activities have some simple computations, but, the computations are not dependent on any activity. 在我拥有的android项目中,所有活动都有一些简单的计算,但是,这些计算不依赖于任何活动。 They depend on various sensor values. 它们取决于各种传感器值。 For some values of the sensors, a new activity is started. 对于传感器的某些值,将开始新的活动。 For the computations in the new activity, values from the previous activity are also required. 对于新活动中的计算,还需要来自先前活动的值。 I can pass the data in the intent, but in the transition time between starting the new activity, I miss some sensor values. 我可以按意图传递数据,但是在开始新活动之间的过渡时间内,我错过了一些传感器值。

So as a workaround, I start a service that performs the computation. 因此,作为一种解决方法,我启动了执行计算的服务。 All the activities implement the method to start a new activity. 所有活动都实现了开始新活动的方法。 Then the activity binds to the service and the service calls the method to start a new activity based on the sensor values. 然后,活动绑定到服务,并且服务根据传感器值调用该方法以启动新活动。 This seems to work fine and help with the data not being lost. 这似乎可以很好地工作,并且有助于避免数据丢失。

However, I am not sure that is this a good practice? 但是,我不确定这是一个好习惯吗? And is there a simpler way to do this? 有没有更简单的方法可以做到这一点?

I like your thinking of handling the launches of each activity from a service based on sensor values. 我喜欢您的想法,即根据传感器值处理服务中每个活动的启动。 However, from the perspective of Android, this is open to severe memory leaking issues. 但是,从Android的角度来看,这存在严重的内存泄漏问题。

If I have understood correctly, you are launching an activity based on the values retained by the service, which is completely fine. 如果我理解正确,那么您将根据服务保留的值启动活动,这完全可以。 However, can you think of any possibility in which you are doing a network call or any other asynchronous work in your activity which is launched? 但是,您能想到正在发起的活动中进行网络呼叫或任何其他异步工作的任何可能性吗? Then what about the background service launches another activity in the middle of that asynchronous call and then launches the new activity? 然后,后台服务在该异步调用的中间启动另一个活动,然后启动新活动呢? The asynchronous call will return with some values to be provided to the calling activity which is out of context right? 异步调用将返回一些要提供给调用活动的值,这是上下文无关的吗? This will cause a memory leak and hence it may crash your application in some places. 这将导致内存泄漏,因此可能会使您的应用程序在某些地方崩溃。

I would like to suggest to maintain a queue for these activity switching operations in which you can handle the activity switching in more sophisticated ways. 我建议为这些活动切换操作维护一个队列,在其中您可以以更复杂的方式处理活动切换。 Just wait for an ongoing asynchronous call to be finished and return with some values to the calling activity and then launch the next activity. 只需等待正在进行的异步调用完成,然后向调用活动返回一些值,然后启动下一个活动即可。 Whenever a data comes from your sensor, push the command for launching the new activity in a queue and then launch the new activity instantly if there is no current asynchronous call is running. 每当数据来自传感器时,请在队列中推送用于启动新活动的命令,然后在没有当前异步调用正在运行的情况下立即启动新活动。

Hope that helps! 希望有帮助!

Update 更新资料

so any guides to implement the queue suggestion? 那么有任何实施队列建议的指南吗?

You might consider having a basic queue implementation here. 您可以考虑在此处使用基本的队列实现。 The queue will have the information of the next action to be performed and you will have another flag which will indicate if the action to be performed is safe to perform now. 队列中将包含要执行的下一个操作的信息,您将拥有另一个标志,该标志将指示要执行的操作现在是否可以安全执行。

if I wait for the asynchronous call to return, would I be blocking the UI? 如果我等待异步调用返回,是否会阻塞UI?

It depends on your implementation. 这取决于您的实现。 I am not quite sure what you are talking about as I am not very aware of the overall application design of yours. 我不太确定您在说什么,因为我不太了解您的总体应用程序设计。

However, I think you need to look into RxJava which enables Android to work on an event-driven mechanism. 但是,我认为您需要研究RxJava ,它可以使Android在事件驱动机制下工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM