简体   繁体   English

在Android TabHost应用程序中完成活动间通信的最佳方式

[英]Best way to accomplish inter-activity communication in an Android TabHost application

Here's the deal: I have an Android application that needs to call a web service every X seconds (currently 60 seconds). 这是交易:我有一个Android应用程序需要每X秒(当前60秒)调用一个Web服务。 This application has multiple tabs and these tabs all need to interact with the data themselves. 此应用程序有多个选项卡,这些选项卡都需要与数据本身进行交互。 One is a MapView, one is a ListView and then the third is irrelevant but will need to also get some global data eventually. 一个是MapView,一个是ListView,然后第三个是无关紧要的,但最终还需要得到一些全局数据。 The issue is that I want my main activity to have a thread that runs in the background, gets the results and then instructs both child activities in the TabHost to update themselves with the latest data. 问题是我希望我的主要活动有一个在后台运行的线程,获取结果,然后指示TabHost中的两个子活动使用最新数据更新自己。 Also, when the user clicks on the tabs and the onCreate/onResume activities fire, I would also like to force a redraw by getting the latest data from the main activity. 此外,当用户单击选项卡并触发onCreate / onResume活动时,我还想通过从主活动获取最新数据来强制重绘。 I'm really at a loss here. 我真的很茫然。 I've tried this with a service and some ghetto static methods to pass an instance of the Activities to the Service to call specific functions to update their views whenever the timer fired, but the slowdowns were pretty bad and the code was just ugly ugly ugly. 我已尝试使用服务和一些ghetto静态方法将活动的实例传递给服务,以便在定时器触发时调用特定函数来更新其视图,但减速非常糟糕且代码只是丑陋难看丑陋。 Any suggestions? 有什么建议么?

edit: So I implemented it as a timer-driven thread in the tabhost activity and then I have timer-driven threads in each child activity that then grab the data (in a synchronized fashion) and update their map/list. 编辑:所以我在tabhost活动中将其实现为计时器驱动的线程,然后我在每个子活动中都有计时器驱动的线程,然后获取数据(以同步的方式)并更新它们的地图/列表。 It's much faster but still feels slightly hack-ish, especially the part where I'm calling a custom function in the parent activity like so: 它的速度要快得多,但仍然感觉有点破解,尤其是我在父活动中调用自定义函数的部分,如下所示:

((MainActivity)getParent()).getNearbyMatches();

This adds an element of strong coupling that I'm not entirely thrilled with, but from a performance standpoint it's much better than it was. 这增加了一个强烈耦合的元素,我并不十分兴奋,但从性能的角度来看,它比它更好。 I appreciate the answers that have already been given and will do a bit of research on the content provider front but I'm not sure I want to go back to the service model. 我很欣赏已经给出的答案,并将对内容提供商进行一些研究,但我不确定是否要回到服务模型。

So I've found what I believe is the answer: The Application Class . 所以我找到了我认为的答案: 应用程序类 You can extend this class to keep track of global application state. 您可以扩展此类以跟踪全局应用程序状态。

In the AndroidManifest.xml file you can reference your fully qualified custom class in the android:name attribute and it will be instantiated when the app fires up. AndroidManifest.xml文件中,您可以在android:name属性中引用完全限定的自定义类,并在应用程序启动时实例化它。

Any Activity can then call "getApplication()" and it will return the instance of your custom Application class, which you can then tailor to taste. 然后,任何Activity都可以调用"getApplication()" ,它将返回自定义Application类的实例,然后您可以根据需要进行定制。

Why are you updating all the children activities every time new data is available? 每次有新数据时,为什么要更新所有子活动? That sounds inefficient to me. 这对我来说听起来效率低下。 Update only the activity that is currently visible. 仅更新当前可见的活动。

One possible way to do this is through a custom content provider . 一种可行的方法是通过自定义内容提供商 Let your service update the data source to your activities and get the current visible activity to listen to changes on this content. 让您的服务更新您的活动的数据源,并获取当前可见的活动以收听此内容的更改。 So basically, your register to the content provider when OnResume is called and unregister when OnPause is called. 所以基本上,你在调用OnResume时注册到内容提供者,并在调用OnPause时注销。

As a rule of thumb never store static references of an Activity!! 根据经验,从不存储Activity的静态引用!! You'll end up with ugly leaks. 你最终会发现丑陋的漏洞。 If it is a must for your application then at least use WeakReferences 如果它是您的应用程序必须的,那么至少使用WeakReferences

You can implement your GUI updates in Handler s and register the Handler instances with your download-thread. 您可以在Handler实现GUI更新,并使用下载线程注册Handler实例。 The download thread then sends messages to the handlers when new data arrives. 然后,下载线程在新数据到达时向处理程序发送消息。 Essentially this is the Observer Pattern . 基本上这是观察者模式 You can find an example of how to use Handler s here (expand the 'Example ProgressDialog with a second thread' section). 您可以在此处找到如何使用Handler的示例(展开'带有第二个线程的示例ProgressDialog'部分)。

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

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