Android 仅当它在前台时才从 BroadcastReceiver 通知活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2282435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inform Activity from a BroadcastReceiver ONLY if it is in the foreground
提问by Zordid
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
等待被AlarmMangager触发 - 这很好用。
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 onlyif it is currently in the foreground, aka active.
现在:因为该事件(如果发生)需要刷新主 Activity 屏幕上的某些元素,我想从该后台 BroadcastReceiver 向我的 Activity 发送一个 Intent -但前提是它当前处于前台,即活动状态。
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.
如果它没有运行或不可见,我不在乎 - 我想做的最后一件事就是按照我的意图启动 Activity!我在 onResume() 方法中处理视图的重绘,所以我根本不在乎。
Any hints on how to do that? Thanks!
关于如何做到这一点的任何提示?谢谢!
EDIT: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.
编辑:我的 BroadcastReceiver 正在等待必须通知用户的警报。因此,它必须存在并在清单中声明。问题是:它必须决定所提到的 Activity 当前是否在前面。
回答by Binh Tran
I believe that you're familiar with AlarmManager now (creating a new Alarm, register a receiver...) so I will not talk about that. Just give you a solution for your question.
我相信你现在对 AlarmManager 很熟悉(创建一个新的警报,注册一个接收器......)所以我不会谈论那个。只是为你的问题提供一个解决方案。
Instead of registering a BroadcastReceiver in a class file and in manifest, you only create a new BroadcastReceiver in your activity, and then, register it in onResume method, and unregister it in onPause method, sth like this in your activity:
无需在类文件和清单中注册 BroadcastReceiver,您只需在您的活动中创建一个新的 BroadcastReceiver,然后在 onResume 方法中注册它,并在 onPause 方法中取消注册,在您的活动中像这样:
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//do something
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mIntentFilter = new IntentFilter();
mIntentFilter.addAction("your alarm action");
...
}
@Override
protected void onResume() {
registerReceiver(mIntentReceiver, mIntentFilter);
...
super.onResume();
}
@Override
protected void onPause() {
unregisterReceiver(mIntentReceiver);
...
super.onPause();
}
The receiver will only receive the alarm intent when your activity is in foreground :)
当您的活动处于前台时,接收器只会收到警报意图 :)
(Sorry if my English is not clear)
(对不起,如果我的英语不清楚)
回答by Chris Boyle
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.
所以这几乎是 Bino 的答案,但是:不要将接收器移动到活动中,而是使用两个具有不同意图的接收器。第一个是您的原始警报意图,在清单中注册了一个接收器,因为您已经拥有,然后该接收器发送第二个广播意图,由活动注册的接收器处理,如 Bino 所说。
I've done this in my own timer project, on github. Here are the alarm receiverand the requery receiver. Hope that helps.