简体   繁体   English

如何在Android中以受控方式将数据发送到多个活动?

[英]how to send data to multiple activities in controlled manner in Android?

I am working on an android application, where there are 4 activities. 我正在使用android应用程序,其中有4个活动。 Activity1 is main activity and there are different buttons on that activity which can open other activities. Activity1是主要活动,该活动上有不同的按钮可以打开其他活动。 Other 3 activities can also open each other. 其他3个活动也可以互相打开。 There is a thread running in Activity1 which returns some counter. Activity1中有一个正在运行的线程,它返回一些计数器。 I need to show that counter on all activities. 我需要在所有活动中都显示该计数器。

At an specific time, the thread doesn't know which activity is at top. 在特定时间,线程不知道哪个活动是最重要的。 What is the right way to control this scenario, such that thread output should update on all activities, no matter which one is at top? 什么是控制此方案的正确方法,以使线程输出应在所有活动上更新,而不管哪个活动在最前面?

I would have that counter on a Service and I would send updates through broadcast intent. 我将在服务上使用该计数器,并通过广播意图发送更新。

Each Activity will register a listener to that intent on the onStart method and unregister it on onPause . 每个Activity将在onStart方法上注册一个针对该意图的侦听器,并在onPause上注销它。 This way the update will reach only the Activity that is on screen. 这样,更新将仅到达屏幕上的活动。

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

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