检查活动是否处于活动状态,以及是否有活动的数据从服务到活动?
我有一个名为EventReceivingSevice
的服务,该服务将在ondatarefresh(jsonobject data)
函数中获取新数据。
private void onNewData(JSONData data) {
boolean isActive=isActivityActive(); //Check if activity is active
if(isACtive)
passData(data);
else
storeData(data);
}
一个称为homeactivity
的活动将显示数据。当EventReceivingService
将获得新数据时,必须检查HOMEACTIVITITY
处于活动状态时,它必须将数据传递给homeactivity
,并且它将将数据存储在某个地方,以便homeactivity
以后将使用它。
数据的格式为JSON。
如何实现这一目标?
I have a service called EventReceivingSevice
which will get new data in onDataRefresh(JSONObject data)
function.
private void onNewData(JSONData data) {
boolean isActive=isActivityActive(); //Check if activity is active
if(isACtive)
passData(data);
else
storeData(data);
}
An activity called HomeActivity
will display the data. When EventReceivingService
will get new data, it has to be checked if HomeActivity
is active, it has to pass the data to HomeActivity
, and if not it will store the data somewhere so that HomeActivity
will later use that.
The data is in JSON format.
How can achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法可靠地确定
活动
是否处于活动状态。您应该做的是将数据存储在某个地方(文件,sqlite数据库等),然后发送一个广播意图
,该表示“新数据可用”。您的活动
可以注册一个侦听器,如果广播当广播 time 意图发送。然后,它可以从任何放置的地方拾取数据,并随心所欲地做任何您想做的事情。You can't reliably determine if an
Activity
is active. What you should do is to store the data somewhere (file, SQLite database, etc.), and then send a broadcastIntent
that means "new data is available". YourActivity
can register a listener that will get triggered by that broadcastIntent
if theActivity
is alive when the broadcastIntent
is sent. It can then pick up the data from wherever you put it and do whatever you want with it.有一个简单的方法,但不需要JSON数据。您可以将数据添加为公共静态数据。
如果您将这些数据用于阅读目的,则可以这样做。
There is a simple method but it doesn't require JSON data. you can just add data as public static data.
If you are using that data for reading purposes, you can do it like this.