如何从 BroadCastReceiver 更新 Activity 的 UI [英] How to update the UI of Activity from BroadCastReceiver
问题描述
我正在学习 Android
概念 Activity
和 BroadCastReceiver
.我想从 BroadtCastReceiver
更新 Activity
的内容,两者都在不同的 java 类中.
I am learning Android
concepts Activity
and BroadCastReceiver
. I want to update the content of Activity
from the BroadtCastReceiver
both are in different java class.
有点像
MyActivity.java
和 MyBroadtCastReceiver.java
这是否可以在 Android 中执行此操作?
Is this possible to do this in Android ?
推荐答案
BroadcastReceiver
可以以多种方式使用,但是当涉及到更新 的 UI 组件等特定事项时Activity
,在它自己的Java类文件中声明/定义一个BroadcastReceiver
没有什么好处.
A BroadcastReceiver
can be used in many ways but when it comes to something as specific as updating the UI components of an Activity
, there is little advantage to declaring / defining a BroadcastReceiver
in it's own Java class file.
推理 - BroadcastReceiver
必须对 Activity
有一些先验的知识",以及它需要做什么才能更新 UI.实际上,BroadcastReceiver
绑定到 Activity
本身,将其声明/定义为内部类是有意义的.
Reasoning - the BroadcastReceiver
has to have some prior "knowledge" of the Activity
and what it is required to do in order to update the UI. In effect the BroadcastReceiver
is tied to the Activity
itself and it makes sense to declare / define it as an inner class.
另一个重要方面是 Activity
需要处于运行"(即可见)状态,以保证对 UI 组件的操作.在这种情况下,在 onResume()
中注册接收器并在 onPause()
中取消注册将有助于防止出现问题.
Another important aspect is the Activity
needs to be in a "running" (i.e., visible) state in order to guarantee manipulation of UI components. In this case, registering the receiver in onResume()
and unregistering in onPause()
will help prevent problems.
使用通用模板我会做类似下面的事情...
Using a generic template I'd do something like the following...
class MyActivity extends Activity {
boolean mIsReceiverRegistered = false;
MyBroadcastReceiver mReceiver = null;
// onCreate(...) here
@Override
protected void onResume() {
// Other onResume() code here
if (!mIsReceiverRegistered) {
if (mReceiver == null)
mReceiver = new MyBroadcastReceiver();
registerReceiver(mReceiver, new IntentFilter("YourIntentAction"));
mIsReceiverRegistered = true;
}
}
@Override
protected void onPause() {
if (mIsReceiverRegistered) {
unregisterReceiver(mReceiver);
mReceiver = null;
mIsReceiverRegistered = false;
}
// Other onPause() code here
}
private void updateUI(Intent intent) {
// Do what you need to do
}
private class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
updateUI(intent);
}
}
}
一些额外的注释...
BroadcastReceiver
的生命周期介于进入和离开onReceive(...)
之间.一旦它从onReceive(...)
返回,实例就会保持休眠状态,等待下一次广播.- 与第 1 点直接相关 -
BroadcastReceiver
不是为繁重工作"而设计的.基本上onReceive(...)
方法应该尽可能简单.它调用的任何方法也应该尽可能轻量级……进入,做你的事情,离开然后等待下一次广播.如果更新 UI 需要一些时间(例如,通过重新查询数据库获取大量数据来更新ListView
),请考虑调用异步执行的代码(AsyncTask
例如).
- The life-cycle of a
BroadcastReceiver
is between entering and leavingonReceive(...)
. Once it has returned fromonReceive(...)
the instance remains in a dormant state waiting for the next broadcast. - Directly related to point 1 - a
BroadcastReceiver
isn't designed for "heavy lifting". Basically theonReceive(...)
method should be kept as simple as possible. Any methods it calls should also be as light-weight as possible...get in, do your stuff, get out then wait for the next broadcast. If updating the UI is going to take some time (perhaps updating aListView
by re-querying a database for a large amount of data for example), consider calling code which performs asynchronously (anAsyncTask
for example).
这篇关于如何从 BroadCastReceiver 更新 Activity 的 UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!