在 Android TabHost 应用程序中完成交互通信的最佳方式

发布于 2024-07-29 15:57:56 字数 675 浏览 2 评论 0原文

事情是这样的:我有一个 Android 应用程序,需要每 X 秒(当前为 60 秒)调用一次 Web 服务。 该应用程序有多个选项卡,这些选项卡都需要与数据本身进行交互。 一个是 MapView,一个是 ListView,第三个是不相关的,但最终还需要获取一些全局数据。 问题是我希望我的主要活动有一个在后台运行的线程,获取结果,然后指示 TabHost 中的两个子活动使用最新数据更新自身。 另外,当用户单击选项卡并且 onCreate/onResume 活动触发时,我还想通过从主活动获取最新数据来强制重绘。 我真的很不知所措。 我已经尝试过使用服务和一些贫民区静态方法来将活动的实例传递给服务,以在计时器触发时调用特定函数来更新其视图,但速度减慢非常严重,而且代码也丑陋丑陋。 有什么建议么?

编辑:所以我将其实现为 tabhost 活动中的计时器驱动线程,然后在每个子活动中都有计时器驱动的线程,然后获取数据(以同步方式)并更新其映射/列表。 它速度更快,但仍然感觉有点黑客风格,尤其是我在父活动中调用自定义函数的部分,如下所示:

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

这增加了一个强耦合的元素,我对此并不完全感到兴奋,但从性能的角度来看现在比以前好多了。 我很欣赏已经给出的答案,并将在内容提供商方面进行一些研究,但我不确定是否想回到服务模型。

Here's the deal: I have an Android application that needs to call a web service every X seconds (currently 60 seconds). 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. 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. 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. 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. 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. 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.

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

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

发布评论

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

评论(3

红衣飘飘貌似仙 2024-08-05 15:57:56

所以我找到了我认为的答案:应用程序类。 您可以扩展此类来跟踪全局应用程序状态。

AndroidManifest.xml 文件中,您可以在 android:name 属性中引用完全限定的自定义类,它将在应用程序启动时实例化。

然后,任何 Activity 都可以调用“getApplication()”,它将返回自定义 Application 类的实例,然后您可以根据需要进行定制。

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.

Any Activity can then call "getApplication()" and it will return the instance of your custom Application class, which you can then tailor to taste.

北音执念 2024-08-05 15:57:56

为什么每次有新数据可用时都要更新所有儿童活动? 这对我来说听起来效率很低。 仅更新当前可见的活动。

一种可能的方法是通过自定义内容提供程序。 让您的服务将数据源更新到您的活动,并获取当前可见的活动来侦听此内容的更改。 所以基本上,您在调用 OnResume 时注册到内容提供程序,并在调用 OnPause 时取消注册。

根据经验,永远不要存储 Activity 的静态引用!! 你最终会遇到丑陋的泄漏。 如果您的应用程序必须这样做,那么至少使用 弱引用

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.

As a rule of thumb never store static references of an Activity!! You'll end up with ugly leaks. If it is a must for your application then at least use WeakReferences

鹿港巷口少年归 2024-08-05 15:57:56

您可以在 Handler 并注册 Handler 实例与您的下载线程。 然后,当新数据到达时,下载线程向处理程序发送消息。 本质上,这就是观察者模式
您可以找到如何使用 Handler 的示例 此处(展开“带有第二个线程的示例 ProgressDialog”部分)。

You can implement your GUI updates in Handlers and register the Handler instances with your download-thread. 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 Handlers here (expand the 'Example ProgressDialog with a second thread' section).

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