Android 架构组件 LiveData - 如何将 broadcastReceivers 绑定到生命周期
Android Architecture component LiveData - how to bind broadcastReceivers to lifecycle
使用 Android LiveData 我希望能够在 onInactive()
和 onActive()
回调中注销和注册许多 BroadcastReceiver
。所以我想做这样的事情:
public class BroadcastRecieverLiveData extends LiveData<BroadCastReciever> {
private BroadcastReciever reciever;
private Context context;
public BroadcastRecieverLiveData(Context context) {
this.context = context;
}
@Override
protected void onActive() {
IntentFilter filter = new IntentFilter();
filter.addAction("SOME_ACTION");
filter.addAction("SOME_OTHER_ACTION");
reciever = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//do something based on the intent's action
}
};
registerReceiver(reciever, filter);
}
@Override
protected void onInactive() {
if (reciever != null) {
context.unregisterReceiver(reciever);
}
}
}
我在想这可能是一种清理代码而不是依赖 onDestroy
的设计模式。您对以这种方式使用 LiveData 有何看法?有一个使用它的例子here
我认为对于接收者,你应该实施LifecycleObserver
。
根据来自 Google codelab、
的 LiveData
文档
Caution: Storing a reference to a Context or View in a ViewModel can
result in memory leaks. Avoid fields that reference instances of the
Context or View classes. The onCleared() method is useful to
unsubscribe or clear references to other objects with a longer
lifecycle, but not for clearing references to Context or View objects.
因此,您不应在 LiveData 中进行上下文密集型操作。
相反,以下面的实现为例,
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.OnLifecycleEvent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
public class ReceiverManager implements LifecycleObserver {
private final Context mContext;
private final MyBrodacastReceiver myBrodacastReceiver;
public ReceiverManager(LifecycleOwner lifecycleOwner,
Context context) {
mContext = context;
myBrodacastReceiver = new MyBrodacastReceiver();
lifecycleOwner.getLifecycle().addObserver(this);
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
void registerYourReceiver() {
mContext.registerReceiver(myBrodacastReceiver, new IntentFilter());
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
void unRegisterYourReceiver() {
mContext.unregisterReceiver(myBrodacastReceiver);
}
private static class MyBrodacastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
}
}
}
谢谢。
使用 Android LiveData 我希望能够在 onInactive()
和 onActive()
回调中注销和注册许多 BroadcastReceiver
。所以我想做这样的事情:
public class BroadcastRecieverLiveData extends LiveData<BroadCastReciever> {
private BroadcastReciever reciever;
private Context context;
public BroadcastRecieverLiveData(Context context) {
this.context = context;
}
@Override
protected void onActive() {
IntentFilter filter = new IntentFilter();
filter.addAction("SOME_ACTION");
filter.addAction("SOME_OTHER_ACTION");
reciever = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//do something based on the intent's action
}
};
registerReceiver(reciever, filter);
}
@Override
protected void onInactive() {
if (reciever != null) {
context.unregisterReceiver(reciever);
}
}
}
我在想这可能是一种清理代码而不是依赖 onDestroy
的设计模式。您对以这种方式使用 LiveData 有何看法?有一个使用它的例子here
我认为对于接收者,你应该实施LifecycleObserver
。
根据来自 Google codelab、
LiveData
文档
Caution: Storing a reference to a Context or View in a ViewModel can result in memory leaks. Avoid fields that reference instances of the Context or View classes. The onCleared() method is useful to unsubscribe or clear references to other objects with a longer lifecycle, but not for clearing references to Context or View objects.
因此,您不应在 LiveData 中进行上下文密集型操作。
相反,以下面的实现为例,
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.OnLifecycleEvent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
public class ReceiverManager implements LifecycleObserver {
private final Context mContext;
private final MyBrodacastReceiver myBrodacastReceiver;
public ReceiverManager(LifecycleOwner lifecycleOwner,
Context context) {
mContext = context;
myBrodacastReceiver = new MyBrodacastReceiver();
lifecycleOwner.getLifecycle().addObserver(this);
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
void registerYourReceiver() {
mContext.registerReceiver(myBrodacastReceiver, new IntentFilter());
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
void unRegisterYourReceiver() {
mContext.unregisterReceiver(myBrodacastReceiver);
}
private static class MyBrodacastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
}
}
}
谢谢。