[英]Updating Activity UI component from Broadcast Receiver?
我有非常基本的问题。 这可能很简单,但我不明白。 我有一个正在使用一些UI组件的Activity。 而且我还有一个广播接收器(从清单中注册) ,在这里我需要更新Activity类的一些UI组件。 喜欢 -
Class MyActivity extends Activity
{
onCreate(){
//using some UI component lets say textview
textView.setText("Some Text");
}
updateLayout()
{
textView.setText("TextView Upadated...");
}
}
Class broadCastReceiver
{
onReceive()
{
//here I want to update My Activity component like
UpdateLayout();
}
}
为此, 一种解决方案是使updateLayout()方法成为公共静态方法 ,并通过活动引用在接收器类中使用该方法。 但是我认为,这不是正确的方法。是否有适当的方法?
可以在运行时注册和注销它,而不是在清单中注册接收者:
另外,请确保您在意图过滤器中以正确的操作注册了接收器。
public class MyActivity extends Activity{
// used to listen for intents which are sent after a task was
// successfully processed
private BroadcastReceiver mUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
new UpdateUiTask().execute();
}
};
@Override
public void onResume() {
registerReceiver(mUpdateReceiver, new IntentFilter(
YOUR_INTENT_ACTION));
super.onResume();
}
@Override
public void onPause() {
unregisterReceiver(mUpdateReceiver);
super.onPause();
}
// used to update the UI
private class UpdateUiTask extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(Void... voids) {
Context context = getApplicationContext();
String result = "test";
// Put the data obtained after background task.
return result;
}
@Override
protected void onPostExecute(String result) {
// TODO: UI update
}
}
}
希望能帮助到你。
我当然会推荐GreenRobot的EventBus
EventBus是Android优化的发布/订阅事件总线。 Android应用程序的一个典型用例是将“活动”,“片段”和“后台线程”粘合在一起。 这些元素的常规布线通常会引入复杂且容易出错的依赖性以及生命周期问题。 随着EventBus在所有参与者之间传播的侦听器(例如,后台服务->活动->多个片段或帮助程序类)已被弃用。 EventBus使事件发送方和接收方分离,从而简化了应用程序组件之间的通信。 代码更少,质量更高。 而且您不需要实现单个接口!
您可以将ObservableEvent与Extended Observable类一起使用。
public class ObservableEvent extends Observable {
public void setChanged(){
super.setChanged();
}
}
制作单例课程通知中心
public class NSNotificationCenter {
private HashMap<String, ObservableEvent> observables = new HashMap<String, ObservableEvent>();
/**
* Lazy load the event bus
*/
public void addObserver(String notification, Observer observer) {
ObservableEvent observable = observables.get(notification);
if (observable == null) {
observable = new ObservableEvent();
observables.put(notification, observable);
}
observable.addObserver(observer);
}
public void removeObserver(String notification, Observer observer) {
Observable observable = observables.get(notification);
if (observable != null) {
observable.deleteObserver(observer);
}
}
public void postNotification(String notification, Object object) {
ObservableEvent observable = observables.get(notification);
if (observable != null) {
observable.setChanged();
if (object == null) {
observable.notifyObservers();
} else {
observable.notifyObservers(object);
}
}
}
}
仅用于添加观察者以观察通知,而触发方法则使用后通知
从文档 :
您可以使用Context.registerReceiver()动态注册此类的实例,也可以通过AndroidManifest.xml中的标签静态发布实现。
在Activity.onResume()
实现中注册接收者,并在Activity.onPause()
注销。
public class MyActivity extends Activity
{
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
MyActivity.this.broadcastReceived(intent);
}
};
public void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter();
// add action, category, data to intentFilter
this.registerReceiver(this.mBroadcastReceiver, intentFilter);
}
public void onPause() {
super.onPause();
this.unregisterReceiver(this.mBroadcastReceiver);
}
public void broadcastReceived(Intent intent) {
// Update the UI component here.
}
}
您可以使用观察者模式-活动在广播接收器的onResume()中注册自己,以接收更新,而在onPause()中注销自身。
网上有很多材料可以学习观察者模式-http: //en.wikipedia.org/wiki/Observer_pattern
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.