Android Architecture组件LiveData-如何将广播接收器绑定到生命周期 [英] Android Architecture component LiveData - how to bind broadcastReceivers to lifecycle

查看:264
本文介绍了Android Architecture组件LiveData-如何将广播接收器绑定到生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Android LiveData 我想成为能够在onInactive()onActive()回调中注销并注册许多BroadcastReceiver.所以我想做这样的事情:

Using Android LiveData I'd like to be able to unregister and register many BroadcastReceivers in the onInactive() and onActive() call backs. 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);
        }
    }
}

我当时认为这可能是一种清理代码而不是在onDestroy上进行中继的设计模式.您对以这种方式使用LiveData有什么想法?有一个使用它的示例在这里

I was thinking this could be a design pattern to be clean up of code instead of relaying on onDestroy. What are your thoughts on using LiveData this way? There is an example of using it here

推荐答案

我认为对于接收者,您应该实现LifecycleObserver. 根据Google LiveData文档"rel =" noreferrer>代码实验室

I think for receivers, you should implement LifecycleObserver. As per LiveData documentation from Google codelab,

警告:在ViewModel中存储对上下文或视图的引用可以 导致内存泄漏.避免引用引用实例的字段 上下文或视图类. onCleared()方法可用于 取消订阅或清除对较长对象的引用 生命周期,但不用于清除对Context或View对象的引用.

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中执行上下文密集型操作.

So, You should not do context intensive operation in 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 Architecture组件LiveData-如何将广播接收器绑定到生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆