Android 本地广播 LocalBroadcastManager 源码解析

序言

Broadcast作为Android的四大组件之一,重要性不言而喻;一般我们使用广播的方式通常如下,继承BroadcastReceiver,新建一个广播类。

public class MyBroadcastReceiver extends BroadcastReceiver { 

   public static final String TAG = "MyBroadcastReceiver"; 

   @Override 
   public void onReceive(Context context, Intent intent) { 

   }
}

然后在Activity中注册

IntentFilter filter = new IntentFilter();
filter.addAction("ceshi");
registerReceiver(new MyBroadcastReceiver(), filter);

在需要的时候发送

Intent intent = new Intent();
intent.setAction("ceshi");
sendBroadcast(intent);

这个时候就会回调MyBroadcastReceiver的onReceive方法,然后在方法中处理你的逻辑, 但是这种方式的广播是系统级别的,就是说如果我在A,B App中都注册了action=”ceshi”的广播,然后A App发送的action=”ceshi”的广播不止会回调A App中的onReceive方法,也会回调B App中的onReceive方法, 就好比:我们所有的App都注册了系统的Wifi状态切换广播,那Wifi状态有变化的时候,所有App都会收到这条广播。

但是有时候我们的业务需求只需要在自己App中发送接受广播就可以了,没必要用系统级别广播,那还有其它方法可以实现这种需求吗?当然有了,就是我们今天要讲的LocalBroadcastManager,从字面意思就可以看出它是针对本地广播的,那现在让我们通过源码来看看它内部的实现逻辑。

使用

注册方法:

BroadcastReceiver br = new BroadcastReceiver() {    
   @Override    
   public void onReceive(Context context, Intent intent) {    
   }
};
LocalBroadcastManager.getInstance(this).registerReceiver(br,new IntentFilter());

发送广播方法:

LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent());

解绑方法:

LocalBroadcastManager.getInstance(this).unregisterReceiver(br);

得到实例方法:

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

从上面方法可以看出LocalBroadcastManager是一个单例全局对象,并且它的几个核心方法和系统级别广播的方法调用是一致的,那下面让我们来通过源码来分析一下它几个核心方法的实现。

源码

注册方法源码

public void registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {    
   synchronized (mReceivers) {        
     ReceiverRecord entry = new ReceiverRecord(filter, receiver);        
     ArrayList<IntentFilter> filters = mReceivers.get(receiver);        
     if (filters == null) {            
       filters = new ArrayList<IntentFilter>(1);     
       mReceivers.put(receiver, filters);        
     }        
     filters.add(filter);        
     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);        
    }    
   }
}

首先它new了一个ReceiverRecord对象,ReceiverRecord对象里面有两个3个属性

IntentFilter filter;
BroadcastReceiver receiver;
boolean broadcasting;

然后通过mReceivers.get(receiver)得到一个IntentFilter List 集合, mReceivers是一个Map, key是BroadcastReceiver, value是ArrayList<IntentFilter>,集合为空就new一个然后放到Map中,然后把传进来的IntentFilter对象添加到List集合当中,从这几句代码可以看出来多个IntentFilter可以对应一个BroadcastReceiver;然后传递的IntentFilter action有可能有多个,循环遍历countActions,然后从mActions集合中通过get方法得到ReceiverRecord List集合, mActions是key为action,value为ArrayList<ReceiverRecord>的Map,就是说一个action可以对应多个ReceiverRecord对象,然后把刚才new的ReceiverRecord对象添加到ArrayList<ReceiverRecord>集合当中,注册方法就完了。

注册成功以后就要使用发送方法来发送广播
发送方法源码:

public boolean sendBroadcast(Intent intent) {    
   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);        

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

       int match = receiver.filter.match(action, type, scheme, data,categories, "LocalBroadcastManager");                
       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);                    
    }                
  }           
 }           

   if (receivers != null) {                
     for (int i=0; i<receivers.size(); i++) {       
          receivers.get(i).broadcasting = false;                
     }                

     mPendingBroadcasts.add(new BroadcastRecord(intent, receivers));               
     if (!mHandler.hasMessages(MSG_EXEC_PENDING_BROADCASTS)) {      
         mHandler.sendEmptyMessage(MSG_EXEC_PENDING_BROADCASTS);                
      }                
     return true;           
  }        
 }   
}    
return false;
}

方法的大致逻辑:首先 从mActions Map中根据intent的action得到ReceiverRecord List集合,然后循环集合得到单个ReceiverRecord 对象,如果ReceiverRecord 对象中的filter字段和intent中的属性匹配成功,那么就把该对象添加到新建的receivers List集合中; 最后把intent和receivers 集合封装成BroadcastRecord对象添加到mPendingBroadcasts List集合当中,然后通过handler发送消息,执行executePendingBroadcasts方法

private void executePendingBroadcasts() {    
  while (true) {        
    BroadcastRecord[] brs = null; 
    synchronized (mReceivers) {            
      final int N = mPendingBroadcasts.size(); 
      if (N <= 0) {                
        return; 
      }            

      brs = new BroadcastRecord[N];     
      mPendingBroadcasts.toArray(brs);            
      mPendingBroadcasts.clear(); 
    }       

   for (int i=0; i<brs.length; i++) { 
     BroadcastRecord br = brs[i];            
     for (int j=0; j<br.receivers.size(); j++) {      
         br.receivers.get(j).receiver.onReceive(mAppContext, br.intent);      }       
   }    
  }
}

这个方法首先把mPendingBroadcasts的数据copy到BroadcastRecord[]数组当中,然后清空mPendingBroadcasts集合,循环遍历数组得到BroadcastRecord 对象,再循环BroadcastRecord 中的receivers集合得到单个ReceiverRecord对象,然后回调ReceiverRecord对象中BroadcastReceiver属性的onReceive方法,onReceive方法就是需要我们重写的方法。

有注册就有解绑,最后我们来看看解绑方法

public void unregisterReceiver(BroadcastReceiver receiver) {   
   synchronized (mReceivers) {        
     ArrayList<IntentFilter> filters = mReceivers.remove(receiver);      
     if (filters == null) {            
        return;        
     }        

     for (int i=0; i<filters.size(); i++) {            
       IntentFilter filter = filters.get(i);            
       for (int j=0; j<filter.countActions(); j++) {                
          String action = filter.getAction(j);    
          ArrayList<ReceiverRecord> receivers = mActions.get(action);                
          if (receivers != null) {                    
            for (int k=0; k<receivers.size(); k++) {                        
              if (receivers.get(k).receiver == receiver) {    
                   receivers.remove(k);                            
                   k--;                        
               }                    
             }                    

           if (receivers.size() <= 0) {      
               mActions.remove(action);                    
           }               
          }            
       }       
      }    
   }
}

首先从mReceivers Map中移除receiver,因为注册的时候放到Map中了,移除返回一个filters集合,然后循环遍历集合,得到filter中的action,在mActions Map中通过action得到receivers集合,这个集合中的单个ReceiverRecord对象的BroadcastReceiver属性如果和要解绑的BroadcastReceiver对象 一样,就移除这个ReceiverRecord对象,最后如果receivers集合中的对象都被移除了,那么就从mActions Map中移除当前的这个action。

到此主要的方法已经分析完毕,可以看出LocalBroadcastManager 内部实现主要依赖Map来保存,遍历,移除数据,是在单个App中进行的,所以以后大家在项目中需要广播的时候多使用本地广播。

谢谢阅读,如果大家感觉本文章对你有用,就麻烦点下喜欢。

    原文作者:Android源码分析
    原文地址: https://juejin.im/entry/5879a9738d6d8100587bb99b
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞