简体   繁体   English

Android架构组件LiveData - 如何将broadcastReceivers绑定到生命周期

[英]Android Architecture component LiveData - how to bind broadcastReceivers to lifecycle

Using Android LiveData I'd like to be able to unregister and register many BroadcastReceiver s in the onInactive() and onActive() call backs. 使用Android LiveData我希望能够在onInactive()onActive() onInactive()取消注册并注册许多BroadcastReceiver So I want to do something like this: 所以我想做这样的事情:

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);
        }
    }
}

I was thinking this could be a design pattern to be clean up of code instead of relaying on onDestroy . 我认为这可能是一种设计模式,可以清理代码而不是在onDestroy上转发。 What are your thoughts on using LiveData this way? 您对这种方式使用LiveData有何看法? There is an example of using it here 有使用它的一个例子在这里

I think for receivers, you should implement LifecycleObserver . 我认为对于接收者,您应该实现LifecycleObserver As per LiveData documentation from Google codelab , 按照LiveData从谷歌文档代码实验室

Caution: Storing a reference to a Context or View in a ViewModel can result in memory leaks. 警告:在ViewModel中存储对Context或View的引用可能导致内存泄漏。 Avoid fields that reference instances of the Context or View classes. 避免使用引用Context或View类实例的字段。 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. onCleared()方法对于取消订阅或清除对具有更长生命周期的其他对象的引用非常有用,但不能用于清除对Context或View对象的引用。

So, You should not do context intensive operation in LiveData. 因此,您不应该在LiveData中执行上下文密集型操作。

Instead, take an example of below implementation, 相反,举一个以下实现的例子,

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) {

        }
    }
}

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android MVP和Lifecycle体系结构组件 - Android MVP and Lifecycle architecture component Android架构组件室库 - 如何处理来自DAO的livingata - Android architecture component room library - How to handle livedata from DAO Android体系结构组件(LiveData):一旦更新了Textview,如何从LiveData中清除所有值 - Android Architecture Component(LiveData): How to clear all value from LiveData once it updates a textview 如何使用DataBinding为许多UI字段管理Android MVVM,Android架构组件(LiveData)? - How to manage Android MVVM, Android Architecture component (LiveData) with DataBinding for many UI fields? 导航组件 - Livedata 生命周期问题 - Navigation Component - Problem with Livedata lifecycle Android 架构 LiveData 和存储库 - Android architecture LiveData and Repositories 开机后是否有Android通话活动? 活动的生命周期,BroadcastReceivers。 - Android calling activities after boot? Lifecycle of activities, BroadcastReceivers. 带有实时数据的 Android MVVM 清洁架构 - Android MVVM clean architecture with livedata Android体系结构组件:如何通过ViewModel观察存储库中的LiveData - Android Architecture Components: How is LiveData in the repository observed by a ViewModel 如何在Android体系结构组件中使用RxJava代替LiveData? - How to use RxJava instead of LiveData with android architecture components?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM