在暂停时如何让活动对本地广播作出反应? [英] How to make activity react to local Broadcasts while paused?

查看:210
本文介绍了在暂停时如何让活动对本地广播作出反应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

制作活动(如果有的话)对通过 LocalBroadcastManager 。如果 LocalBroadcastManager 与普通广播不同,我找不到太多细节,但如果活动在后台,它似乎仍然被称为事件。但是这会导致一些问题,因为只要没有显示活动,就不可能进行一些UI修改。

What is the best way to make an activity (if there is any) react to a broadcast that was send over a LocalBroadcastManager while the activity was paused or stopped. I couldn't find much details if the LocalBroadcastManager differs from a normal broadcast, but it seems that is still gets called event if the activity is in background. But that leads to some problems since some UI modifications are not possible as long as the activity is not shown.

这就是为什么我更愿意在活动获得时处理所有广播回到顶部。但是我该怎么做呢我正在考虑一个队列,它将获取在活动恢复运行状态时执行的可运行列表。但是我认为这是某种矫枉过正,因为机器人确实有这样的机制。

That's why I would prefer to handle all broadcasts when the activity gets back to top. But how can I do this. I was thinking of a queue that will take a list of runnables that gets executed when the activity comes back to running state. But I think this is some sort of overkill since android surly does have a mechanism for that.

那么这样做的正确方法是什么?

So whats the proper way to do this?

推荐答案

我过去所做的是让我的活动在onResume上注册一个接收器并取消注册onPause。

What I've done in the past is have my activities register a receiver onResume and unregister onPause.

当您完成证书创建后,保存到本地存储,无论您需要更新视图和发送广播信息。

When you've finished creating your certificates save to local storage whatever information you need to update your view and send out the broadcast.

如果活动正在运行将接收广播并自行更新。如果它被暂停,你应该读取on​​Resume上的本地存储并相应地更新。

If the activity is running it will receive the broadcast and update itself. If it it is paused, you should read the local storage onResume and update accordingly.

这篇关于在暂停时如何让活动对本地广播作出反应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆