美文网首页
Android之LocalBroadCastManager实现原

Android之LocalBroadCastManager实现原

作者: android_coder | 来源:发表于2020-01-02 20:57 被阅读0次

    LocalBroadcastManager实现原理

    1:概述

    我们都知道广播可以用来实现跨进程通信,它的原理就是binder,通过system_server来实现的,那么当我们只是在应用内部来实现通信的时候,其实我们可以考虑使用LocalBroadcastManager来实现,它有两个好处
    1.1:性能较一般的broadcast要好,因为它的实现不需要经过binder调用,它采用的是handler通信来实现的
    1.2:安全性较好,不必担心数据泄露或是被截取
    LocalBroadcastManager是单利模式,并且只能通过代码的方式注册,不能通过xml的方式注册,下面是谷歌对其的解释
    /**

    • Helper to register for and send broadcasts of Intents to local objects
    • within your process.
    • This has a number of advantages over sending
    • global broadcasts with {@link android.content.Context#sendBroadcast}:
    • You know that the data you are broadcasting won't leave your app, so
    • don't need to worry about leaking private data.
    • It is not possible for other applications to send these broadcasts to
    • your app, so you don't need to worry about having security holes they can
    • exploit.
    • It is more efficient than sending a global broadcast through the
    • system.
      */

    2:实现原理

    2.1:单例模式

    @NonNull
    public static LocalBroadcastManager getInstance(@NonNull Context context) {
        synchronized (mLock) { //锁
            if (mInstance == null) {//创建对象
                mInstance = new LocalBroadcastManager(context.getApplicationContext());
            }
            return mInstance;
        }
    }
    
    private LocalBroadcastManager(Context context) {
        mAppContext = context;
        //getMainLooper是主线程的对应的handler对象
        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);
                }
            }
        };
    }
    

    2.2注册广播registerReceiver

    public void registerReceiver(@NonNull BroadcastReceiver receiver,
            @NonNull IntentFilter filter) {
        synchronized (mReceivers) {
            //创建一个ReceiverRecord对象
            ReceiverRecord entry = new ReceiverRecord(filter, receiver);
            //先从缓存的HashMap中查询,key为receiver
            ArrayList<ReceiverRecord> filters = mReceivers.get(receiver);
            if (filters == null) {
                filters = new ArrayList<>(1);
                mReceivers.put(receiver, filters);//缓存到hashmap列表中去
            }
            filters.add(entry);
            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);//缓存到action的Hashmap列表中
                }
                entries.add(entry);
            }
        }
    }
    

    这个过程主要是将数据缓存到两个列表中去

    1: private final HashMap<BroadcastReceiver, ArrayList<ReceiverRecord>> mReceivers
    = new HashMap<>(); key为receiver,value为ReceiverRecord列表

    2: private final HashMap<String, ArrayList<ReceiverRecord>> mActions = new HashMap<>();
    将action缓存到Hashmap中去,记录的是action与广播接收者之间的关系

    2.3:取消注册unregisterReceiver

    public void unregisterReceiver(@NonNull BroadcastReceiver receiver) {
        synchronized (mReceivers) {
            //从ArratList中移除广播接收者,remove方法返回的是value值
            final ArrayList<ReceiverRecord> filters = mReceivers.remove(receiver);
            if (filters == null) {//如果为null的话不做后续处理
                return;
            }
            for (int i=filters.size()-1; i>=0; i--) {
                final ReceiverRecord filter = filters.get(i);
                filter.dead = true;
                for (int j=0; j<filter.filter.countActions(); j++) {
                    final String action = filter.filter.getAction(j);
                    final ArrayList<ReceiverRecord> receivers = mActions.get(action);
                    if (receivers != null) {
                        for (int k=receivers.size()-1; k>=0; k--) {
                            final ReceiverRecord rec = receivers.get(k);
                            if (rec.receiver == receiver) {
                                rec.dead = true;
                                receivers.remove(k);//从mActions移除接收者为空的action
                            }
                        }
                        if (receivers.size() <= 0) {
                            mActions.remove(action);
                        }
                    }
                }
            }
        }
    }
    

    2.4发送广播sendBroadcast

    public boolean sendBroadcast(@NonNull 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);
            //intent读取action,根据action获取相应的广播接收者列表
            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;
                    }
                    //根据intent和接收者信息创建广播对象,添加到mPendingBroadcasts
                    mPendingBroadcasts.add(new BroadcastRecord(intent, receivers));
                    if (!mHandler.hasMessages(MSG_EXEC_PENDING_BROADCASTS)) {
                        mHandler.sendEmptyMessage(MSG_EXEC_PENDING_BROADCASTS);
                    } //发送到主线程对应的消息队列中去
                    return true;
                }
            }
        }
        return false;
    }
    

    2.5:主线程消息队列:消息的处理

    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);
                }
            }
        };
    
    private void executePendingBroadcasts() {
        while (true) {
            final BroadcastRecord[] brs;
            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++) {
                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);//回调广播接收者的onReceive方法
                    }
                }
            }
        }
    }
    

    3:概括

    优点:
    1:相对于串行广播,更加安全,数据只会在应用内部传输,并且限定在同一进程中,一个APK中其他的进程也是不行的
    2:性能较好:相对于串行广播,其不对system_server造成任何的性能问题,并且也没有跨进程调用带来的耗时问题
    缺点:
    1:不支持跨进程,如果跨进程必须考虑其他实现方式
    2:不支持静态xml的方式注册,无法实现监听诸如系统开机广播等

    相关文章

      网友评论

          本文标题:Android之LocalBroadCastManager实现原

          本文链接:https://www.haomeiwen.com/subject/mwxaoctx.html