从一个BroadcastReceiver通知的活动,只有当它是在前景 [英] Inform Activity from a BroadcastReceiver ONLY if it is in the foreground
问题描述
也许很容易,但我真的不能算出这个正确的那么远,我有一个的BroadcastReceiver
等待获得由AlarmMangager触发 - 这工作正常
Maybe it's easy, but I couldn't really figure this out right so far... I got a BroadcastReceiver
waiting to get triggered by the AlarmMangager - this works fine.
现在:因为该事件,如果它发生,需要刷新的主要活动的屏幕一些内容,我想从这样的背景下的BroadcastReceiver发送的意图,我的活动 - 但是只有如果它是目前在前台,又名活性
Now: because the event, if it occurs, needs to refresh some elements on screen of the main Activity, I would like to send an Intent from that background BroadcastReceiver to my Activity - but only if it is currently in the foreground, aka active.
如果它不运行或不可见的,我不在乎 - 我想做的最后一件事是我的意图开始的活动!我把手重新粉刷在我的onResume()方法的意见,所以我不关心的。
If it is not running or not visible, I don't care - and the last thing I want to do is start the Activity by my intent! I handle repainting of the views in my onResume() method, so I don't care at all.
这是如何做到这一点的任何提示? 谢谢!
Any hints on how to do that? Thanks!
编辑:我的BroadcastReceiver等待,必须通知用户报警。因此,它必须在那里,并在清单中声明。现在的问题是:它必须决定所提到的活性当前是否在前面或不
my BroadcastReceiver is waiting for alarms that must be notified to the user. So, it must be there and declared in the manifest. The problem is: it will have to decide whether the mentioned Activity is currently up in front or not.
推荐答案
因此,这几乎是BINO的答案,但,:代替接收搬入活动中,使用两个接收器,具有不同的意图。第一个就是你原来的报警意图,与清单登记,你已经有了,然后在接收端发送第二广播的意图,这是由活动注册的接收处理的接收器BINO说。
So this is almost Bino's answer, but: instead of moving the receiver into the activity, use two receivers, with different Intents. The first one is your original alarm Intent, with a receiver registered in the manifest as you already have, and then that receiver sends a second broadcast intent, which is handled by a receiver registered by the activity as Bino says.
我在我自己的计时器的项目做到了这一点,在GitHub上。这里是报警接收机和<一href="http://github.com/chrisboyle/timer/blob/master/src/name/boyle/chris/timer/TimerActivity.java#L86">requery接收器的。希望有所帮助。
I've done this in my own timer project, on github. Here are the alarm receiver and the requery receiver. Hope that helps.
这篇关于从一个BroadcastReceiver通知的活动,只有当它是在前景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!