LocalBroadcastManager的使用和源码解析

LocalBroadcastManager能够让自己发送的广播不被外部App接收,这样既安全,也又优化了性能,因为传送和接收广播也是跨进程通信。而通过LocalBroadcastManager发送广播和该BroadcastReceiver接收广播都是在自己应用的进程完成的。

接下来我会先简单的说LocalBroadcastManager如何使用,然后在使用的基础上从源码角度讲解这是如何实现的。


1.使用

我的例子很简单,首先是通过单例模式创建LocalBroadcastManager,然后创建BroadcastReceiver并且创建IntentFilter设置过滤规则,最后点击按钮通过LocalBroadcastManager发送广播

public class MainActivity extends AppCompatActivity {


    public static final String IntentFilerTest = "test";

    LocalBroadcastManager localBroadcastManager;
    Button btn;
    MyBroadcastReceiver myBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.setAction(IntentFilerTest);
                localBroadcastManager.sendBroadcast(intent);
            }
        });

        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(IntentFilerTest);
        myBroadcastReceiver = new MyBroadcastReceiver();
        localBroadcastManager.registerReceiver(myBroadcastReceiver, intentFilter);

    }

    public void showToast(String s) {
        Toast.makeText(this, s , Toast.LENGTH_SHORT).show();
    }
    
    class MyBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                String action = intent.getAction();
                if (action == IntentFilerTest) {
                    showToast(IntentFilerTest);
                }
            }
        }
    }


}


2.源码解析

我们按照上面使用LocalBroadcastManager的步骤来讲解源码

首先是LocalBroadcastManager的单例模式,这个单例实现是通过同步锁锁住一个Object类实现线程同步,然后判断mInstance是否为空,为空就初始化


    private static final Object mLock = new Object();
    private static LocalBroadcastManager mInstance;

    public static LocalBroadcastManager getInstance(Context context) {
        synchronized (mLock) {
            if (mInstance == null) {
                mInstance = new LocalBroadcastManager(context.getApplicationContext());
            }
            return mInstance;
        }
    }

看看构造函数,就保存Context实例和创建一个主线程的Handler,关于executePendingBroadcasts()函数我后面说。

    private LocalBroadcastManager(Context context) {
        mAppContext = context;
        mHandler = new Handler(context.getMainLooper()) {

            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG_EXEC_PENDING_BROADCASTS:
                        executePendingBroadcasts();
                        break;
                    default:
                        super.handleMessage(msg);
                }
            }
        };
    }

在我说LocalBroadcastManager如何注册广播之前,我先贴出两个LocalBroadcastManager内部类代码

    //记录过滤规则和广播接收器
private static final class ReceiverRecord {
        final IntentFilter filter;
        final BroadcastReceiver receiver;
       //为true判断该广播接收器正在接收广播,目前不接受广播,为false表示处于空闲状态,接收广播
        boolean broadcasting;
        boolean dead;

        ReceiverRecord(IntentFilter _filter, BroadcastReceiver _receiver) {
            filter = _filter;
            receiver = _receiver;
        }

        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder(128);
            builder.append("Receiver{");
            builder.append(receiver);
            builder.append(" filter=");
            builder.append(filter);
            if (dead) {
                builder.append(" DEAD");
            }
            builder.append("}");
            return builder.toString();
        }
    }
//记录过滤规则中每一个Action和与之对应的ReceiverRecord
    private static final class BroadcastRecord {
        final Intent intent;
        final ArrayList<ReceiverRecord> receivers;

        BroadcastRecord(Intent _intent, ArrayList<ReceiverRecord> _receivers) {
            intent = _intent;
            receivers = _receivers;
        }
    }

注册广播接收器主要依靠以下两个HashMap对象记录信息

    //主要记录IntentFilter和与之配对的BroadcastReceiver
private final HashMap<BroadcastReceiver, ArrayList<ReceiverRecord>> mReceivers
            = new HashMap<>();
//记录发送的广播里的Action与之配对的BroadcastReceiver
 private final HashMap<String, ArrayList<ReceiverRecord>> mActions = new HashMap<>();
现在我们看看注册广播接收器的代码
    public void registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
        synchronized (mReceivers) {
            //首先是将广播接收器和过滤规则保存在ReceiverRecord对象里
            ReceiverRecord entry = new ReceiverRecord(filter, receiver);
           //然后在mReceivers这个HashMap里通过BroadCastReceiver实例去取ReceiverRecord对象,如果没有
取到就将receiver和filter保存在HashMap里,这里我感觉是防止
            ArrayList<ReceiverRecord> filters = mReceivers.get(receiver);
            if (filters == null) {
                filters = new ArrayList<>(1);
                mReceivers.put(receiver, filters);
            }
            filters.add(entry);
            //遍历过滤规则,将其中包含的Action一条一条的取出来,如果通过action获取不到ReceiverRecord,就保存在HashMap里
            for (int i=0; i<filter.countActions(); i++) {
                String action = filter.getAction(i);
                ArrayList<ReceiverRecord> entries = mActions.get(action);
                if (entries == null) {
                    entries = new ArrayList<ReceiverRecord>(1);
                    mActions.put(action, entries);
                }
                entries.add(entry);
            }
        }
    }

虽然两个HashMap里的值都是ArrayList,但是创建的时候都是容量为1,要注意

好,我们现在开始说说LocalBroadcastManager发送广播的代码

    public boolean sendBroadcast(Intent intent) {
       //先通过mReceivers来个同步锁
        synchronized (mReceivers) {
            final String action = intent.getAction();
            final String type = intent.resolveTypeIfNeeded(
                    mAppContext.getContentResolver());
            final Uri data = intent.getData();
            final String scheme = intent.getScheme();
            final Set<String> categories = intent.getCategories();

            final boolean debug = DEBUG ||
                    ((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0);
            if (debug) Log.v(
                    TAG, "Resolving type " + type + " scheme " + scheme
                    + " of intent " + intent);
            //通过Intent设置的Action获取对应的ReceiverRecord对象,
            ArrayList<ReceiverRecord> entries = mActions.get(intent.getAction());
            if (entries != null) {
                if (debug) Log.v(TAG, "Action list: " + entries);

                ArrayList<ReceiverRecord> receivers = null;
                for (int i=0; i<entries.size(); i++) {
                    ReceiverRecord receiver = entries.get(i);
                    if (debug) Log.v(TAG, "Matching against filter " + receiver.filter);
                    //如果广播接收器处于正在接收广播的状态,不接收广播
                    if (receiver.broadcasting) {
                        if (debug) {
                            Log.v(TAG, "  Filter's target already added");
                        }
                        continue;
                    }
                   //判断这个Intent的参数是否符合广播接收器的过滤规则
                    int match = receiver.filter.match(action, type, scheme, data,
                            categories, "LocalBroadcastManager");
                   //如果符合,receivers就将添加这个receiver,并且设置状态为正在接收广播
                   if (match >= 0) {
                        if (debug) Log.v(TAG, "  Filter matched!  match=0x" +
                                Integer.toHexString(match));
                        if (receivers == null) {
                            receivers = new ArrayList<ReceiverRecord>();
                        }
                        receivers.add(receiver);
                        receiver.broadcasting = true;
                    } else {
                        if (debug) {
                            String reason;
                            switch (match) {
                                case IntentFilter.NO_MATCH_ACTION: reason = "action"; break;
                                case IntentFilter.NO_MATCH_CATEGORY: reason = "category"; break;
                                case IntentFilter.NO_MATCH_DATA: reason = "data"; break;
                                case IntentFilter.NO_MATCH_TYPE: reason = "type"; break;
                                default: reason = "unknown reason"; break;
                            }
                            Log.v(TAG, "  Filter did not match: " + reason);
                        }
                    }
                }
                //遍历receivers,将状态改为接收完广播
                if (receivers != null) {
                    for (int i=0; i<receivers.size(); i++) {
                        receivers.get(i).broadcasting = false;
                    }
                   //将Intent和recievers保存在mPendingBroadcasts对象,然后如何Handler的消息队列里没有MSG_EXEC_PENDING_BROADCASTS
类型的消息,就发送该类型的消息
                   mPendingBroadcasts.add(new BroadcastRecord(intent, receivers));
                    if (!mHandler.hasMessages(MSG_EXEC_PENDING_BROADCASTS)) {
                        mHandler.sendEmptyMessage(MSG_EXEC_PENDING_BROADCASTS);
                    }
                    return true;
                }
            }
        }
        return false;
    }

最后我们再看看executePendingBroadcasts()的代码,看看handler是如何处理消息的

    private void executePendingBroadcasts() {
        while (true) {
            final BroadcastRecord[] brs;
            synchronized (mReceivers) {
              //判断mPendingBroadcasts有没有东西
              final int N = mPendingBroadcasts.size();
                if (N <= 0) {
                    return;
                }
                //如果有就转换为数组
                brs = new BroadcastRecord[N];
                mPendingBroadcasts.toArray(brs);
                mPendingBroadcasts.clear();
            }
            //接下里就很简单了,遍历数组取出BroadcastReceiver对象,然后遍历receivers,最后很直接的通过广播接收器的实例调用
          onReceive函数,传递参数完毕
            for (int i=0; i<brs.length; i++) {
                final BroadcastRecord br = brs[i];
                final int nbr = br.receivers.size();
                for (int j=0; j<nbr; j++) {
                    final ReceiverRecord rec = br.receivers.get(j);
                    if (!rec.dead) {
                        rec.receiver.onReceive(mAppContext, br.intent);
                    }
                }
            }
        }
    }

总结:LocalBroadcastManager是内部是通过Handler实现广播的传递,注册广播时保存BroadcastReceiver和IntentFilter的实例,当发送广播时,保存传递获取过来的Intent,并判断Intent是否符合过滤规则,如果符合再发送消息给Handler,取出BroadcastReceiver调用onReceiver函数,完成广播的接收。

PS:谷歌员工真的很喜欢HashMap保存对象



已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页