转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/41096639 。本文出自:【张鸿洋的博客】

1、概述

关于Eventbus的介绍。前面已经有两篇:Android EventBus实战 没听过你就out了和Android EventBus源代码解析 带你深入理解EventBus 。 假设你觉得还有问题,没关系,接下来我带大家手把手打造从无到有的编写这种框架~~~

首先我们回想一下,这玩意就是在register时,扫描类中复合命名规范的方法,存到一个map,然后post的时候。查找到匹配的方法,反射调用;好,那么依据这一句话。我们就開始编写框架之旅~~~

2、依旧是原来的配方

下面出现的实例代码和Android EventBus实战 没听过你就out了基本一致。所以我就贴出部分

1、ItemListFragment

package com.angeldevil.eventbusdemo;import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;import com.angeldevil.eventbusdemo.Event.ItemListEvent;
import com.zhy.eventbus.EventBus;public class ItemListFragment extends ListFragment
{@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);// RegisterEventBus.getInstatnce().register(this);}@Overridepublic void onDestroy(){super.onDestroy();// UnregisterEventBus.getInstatnce().unregister(this);}@Overridepublic void onViewCreated(View view, Bundle savedInstanceState){super.onViewCreated(view, savedInstanceState);// 开启线程载入列表new Thread(){public void run(){try{Thread.sleep(2000); // 模拟延时// 公布事件,在后台线程发的事件EventBus.getInstatnce().post(new ItemListEvent(Item.ITEMS));} catch (InterruptedException e){e.printStackTrace();}};}.start();}public void onEventUI(ItemListEvent event){setListAdapter(new ArrayAdapter<Item>(getActivity(),android.R.layout.simple_list_item_activated_1,android.R.id.text1, event.getItems()));}@Overridepublic void onListItemClick(ListView listView, View view, int position,long id){super.onListItemClick(listView, view, position, id);EventBus.getInstatnce().post(getListView().getItemAtPosition(position));}}

2、ItemDetailFragment

package com.angeldevil.eventbusdemo;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;import com.zhy.eventbus.EventBus;public class ItemDetailFragment extends Fragment
{private TextView tvDetail;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);// registerEventBus.getInstatnce().register(this);}@Overridepublic void onDestroy(){super.onDestroy();// UnregisterEventBus.getInstatnce().unregister(this);}/** List点击时会发送些事件,接收到事件后更新详情 */public void onEventUI(Item item){if (item != null)tvDetail.setText(item.content);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){View rootView = inflater.inflate(R.layout.fragment_item_detail,container, false);tvDetail = (TextView) rootView.findViewById(R.id.item_detail);return rootView;}
}

能够看到。我们在ItemListFragment里面使用了:

EventBus.getInstatnce().post(new ItemListEvent(Item.ITEMS));去公布了一个事件。然后更新了我们的列表;

点击Item的时候,使用EventBus.getInstatnce().post(getListView().getItemAtPosition(position));公布了一个事件,更新了我们的ItemDetailFragment的列表;

效果:

效果图和之前的一摸一样~~~

可是请注意,如今我们用的是EventBus.getInstatnce()。并发是EventBus.getDefault();而且看下包名import com.zhy.eventbus.EventBus;

我想你应该明确了,这是我们自己写的类来实现的~~~~

好了,接下来就带大家一起实现这个类~~

ps :以上代码和效果图。全然是为了博客的完整性。勿见怪~~

3、无中生有

1、getInstance

我们这里为了方便,直接简单粗暴的使用恶汉模式创建单例:

    private static EventBus eventBus = new EventBus();public static EventBus getInstatnce(){return eventBus;}private EventBus(){mHandler = new Handler(Looper.getMainLooper());}

然后在构造方法中初始化了一个mHandler,没错,它就是用来在处理在UI线程调用方法的。

接下来看register

2、register

/** 我们的强大的map。存储我们的方法*/private static Map<Class, CopyOnWriteArrayList<SubscribeMethod>> mSubscribeMethodsByEventType = new HashMap<Class, CopyOnWriteArrayList<SubscribeMethod>>();public void register(Object subscriber){Class clazz = subscriber.getClass();Method[] methods = clazz.getDeclaredMethods();CopyOnWriteArrayList<SubscribeMethod> subscribeMethods = null;/*** 遍历全部方法*/for (Method method : methods){String methodName = method.getName();/*** 推断方法是否以onEvent的开头*/if (methodName.startsWith("onEvent")){SubscribeMethod subscribeMethod = null;// 方法命中提前在什么线程执行。默认在UI线程String threadMode = methodName.substring("onEvent".length());ThreadMode mode = ThreadMode.UI;Class<?

>[] parameterTypes = method.getParameterTypes(); // 參数的个数为1 if (parameterTypes.length == 1) { Class<?> eventType = parameterTypes[0]; synchronized (this) { if (mSubscribeMethodsByEventType.containsKey(eventType)) { subscribeMethods = mSubscribeMethodsByEventType .get(eventType); } else { subscribeMethods = new CopyOnWriteArrayList<SubscribeMethod>(); mSubscribeMethodsByEventType.put(eventType, subscribeMethods); } } if (threadMode.equals("Async")) { mode = ThreadMode.Async; } // 提取出method,mode,方法所在类对象。存数的类型封装为SubscribeMethod subscribeMethod = new SubscribeMethod(method, mode, subscriber); subscribeMethods.add(subscribeMethod); } } } } enum ThreadMode { UI, Async } class SubscribeMethod { Method method; ThreadMode threadMode; Object subscriber; public SubscribeMethod(Method method, ThreadMode threadMode, Object subscriber) { this.method = method; this.threadMode = threadMode; this.subscriber = subscriber; } }

能够看到我们使用了一个Map存储全部的方法。key为參数的类型class;value为CopyOnWriteArrayList<SubscribeMethod>

这里我们封装了一个SubscribeMethod,这个里面存储了我们须要执行方法的全部參数,毕竟我们执行时。须要该方法,该方法所在的对象。以及在什么线程执行;三个对象足以。当然也缺一不可了~~

register里面,我们遍历该类的全部方法,找到onEvent开头的,封装成SubscribeMethod。存在Map里面。当然了,一个參数类型相应非常多方法,所以value是个CopyOnWriteArrayList。

扫描完毕。我们就完毕了将方法的存储。

另一点,我们这里默认在UI线程执行,假设方法是onEventAsync则觉得在子线程执行,我们也仅仅支持这两种模式,简化一点~

3、post

private static ThreadLocal<PostingThread> mPostingThread = new ThreadLocal<PostingThread>(){@Overridepublic PostingThread get(){return new PostingThread();}};public void post(Object eventTypeInstance){//拿到该线程中的PostingThread对象PostingThread postingThread = mPostingThread.get();postingThread.isMainThread = Looper.getMainLooper() == Looper.myLooper();//将事件增加事件队列List<Object> eventQueue = postingThread.mEventQueue;eventQueue.add(eventTypeInstance);//防止多次调用if (postingThread.isPosting){return;}postingThread.isPosting = true;//取出全部事件进行调用while (!eventQueue.isEmpty()){Object eventType = eventQueue.remove(0);postEvent(eventType, postingThread);}postingThread.isPosting = false;}

我们这里学习了源代码,也搞了个当前线程中的变量,存储了一个事件队列以及事件的状态;

class PostingThread
{List<Object> mEventQueue = new ArrayList<Object>();boolean isMainThread;boolean isPosting;
}

终于公布的事件先增加到事件队列。然后再取出来调用postEvent

private void postEvent(final Object eventType, PostingThread postingThread){CopyOnWriteArrayList<SubscribeMethod> subscribeMethods = null;synchronized (this){subscribeMethods = mSubscribeMethodsByEventType.get(eventType.getClass());}for (final SubscribeMethod subscribeMethod : subscribeMethods){if (subscribeMethod.threadMode == ThreadMode.UI){if (postingThread.isMainThread){invokeMethod(eventType, subscribeMethod);} else{mHandler.post(new Runnable(){@Overridepublic void run(){invokeMethod(eventType, subscribeMethod);}});}} else{new AsyncTask<Void, Void, Void>(){@Overrideprotected Void doInBackground(Void... params){invokeMethod(eventType, subscribeMethod);return null;}};}}}

postEvent也非常easy,直接依据參数类型,去map改到该方法,依据其threadMode。假设在UI线程。则推断当前线程。假设是UI线程,直接调用。否则通过handler执行;

假设非UI线程。这里我们直接开启了一个Thread去执行;

invokeMethod非常easy。就是反射调用方法了~

private void invokeMethod(Object eventType, SubscribeMethod subscribeMethod){try{subscribeMethod.method.invoke(subscribeMethod.subscriber, eventType);} catch (Exception e){e.printStackTrace();}}

4、unregister

public void unregister(Object subscriber){Class clazz = subscriber.getClass();Method[] methods = clazz.getDeclaredMethods();List<SubscribeMethod> subscribeMethods = null;for (Method method : methods){String methodName = method.getName();if (methodName.startsWith("onEvent")){Class<?>[] parameterTypes = method.getParameterTypes();if (parameterTypes.length == 1){synchronized (this){mSubscribeMethodsByEventType.remove(parameterTypes[0]);}}}}}

unregister时。由于我们没有存不论什么的辅助状态,我们仅仅能再去遍历了方法了~~只是通过这个。也能反应出EventBus内部好几个Map的作用了~~

而且。我们也不支持一些状态的查询,还是由于我们没有存一些辅助状态,比如isRegister等等。

到此,我们的EventBus就写好了。100多行代码。肯定没有EventBus健壮,主要目的还是学习人家的思想,经过自己写了这么个类,我相信对于EventBus的理解就更深刻了~面试的时候,恨不得拿仅仅笔写给面试官看,哈哈~~

5、EventBus最佳实践

前面的文章,非常多朋友问。假设我多个方法參数都一样,岂不是post一个此參数,会多个方法调用;而此时我想调用指定的方法怎么办?

还有。项目中会有非常多地方去接收List參数,而List<T>中的泛型是不一致的,所以也可能post(List)时。会调用非常多方法。造成出错。

的确。上述,不加处理肯定会出现;

可是,推荐大家在使用EventBus的时候。创建一个事件类,把你的每个參数(或者可能发生冲突的參数),封装成一个类:

比如:

public class Event
{public static class UserListEvent{public List<User> users ;}public static class ItemListEvent{public List<Item> items;}}

这种话。就不会发生什么调用冲突了~~

源代码点击下载

我建了一个QQ群,方便大家交流。群号:55032675

----------------------------------------------------------------------------------------------------------

博主部分视频已经上线,假设你不喜欢枯燥的文本。请猛戳(初录。期待您的支持):

1、高仿微信5.2.1主界面及消息提醒

2、高仿QQ5.0側滑

3、Android智能机器人“小慕”的实现

Android 框架炼成 教你怎样写组件间通信框架EventBus相关推荐

  1. android mvvm框架搭建_轻松搭建基于JetPack组件的MVVM框架

    原文链接:轻松搭建基于JetPack组件的MVVM框架 - 掘金 Brick github gitee 介绍 辅助android开发者搭建基于JetPack组件构建MVVM框架的注解处理框架.通过注解 ...

  2. php怎么自己写框架,PHP学习笔记,自己动手写个MVC的框架

    最新在大家自己的博客的过程中,发现各种开源的博客系统都或多或少的用起来别扭.于是想动手自己写个博客系统.既然写,就想好好写.那就先写个MVC框架.一点一点来.写的过程中有很多想法.还希望大家能够多多指 ...

  3. Android Service、IntentService,Service和组件间通信

    Service组件 Service 和Activity 一样同为Android 的四大组件之一,并且他们都有各自的生命周期,要想掌握Service 的用法,那就要了解Service 的生命周期有哪些方 ...

  4. 使用Event Bus模式解耦Android App组件间通信

    场景描述 当一个Android应用功能越来越多的时候,保证应用的各个部分之间高效的通信将变得越来越困难. 在应用中的多个地方,控件经常需要根据某个状态来更新他们显示的内容.这种场景常见的解决方式就是定 ...

  5. Android 框架练成 教你打造高效的图片加载框架

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/41874561 ,本文出自: [张鸿洋的博客] 1.概述 优秀的图片加载框架不要 ...

  6. android开发 自我优势_6年Android开发程序员教你如何写简历!看完别再问为何你只值5K...

    每年这个时候,就会有大量的程序猿.攻城狮.产品汪等等准备换一个新环境.而换环境的第一个门槛就是如何写好一份简历 很多小伙伴私下问我,前端都学得差不多啦,想去面试看看,但是简历投出去都石沉大海了. 本篇 ...

  7. android组建之间通信_android组件间通信有哪些方式

    四大组件以及通讯机制: activity (1)一个Activity通常就是一个单独的屏幕(窗口). (2)Activity之间通过Intent进行通信. (3)android应用中每一个Activi ...

  8. android应用组件安全,基于组件间通信的Android应用安全分析

    Android application security analysis based on inter-component communication Huang Yanyi 1 黄炎裔(1994- ...

  9. 简单教你React父子组件间平级组件间传值

    国庆充电特辑: 堵车堵死,废话不多说直接上菜. 1.父组件对子组件传值 利用props属性传值 class Component extends React.Component {constructor ...

最新文章

  1. 什么是随机存取与顺序存取?
  2. 第十三章、facl及用户及Linux终端
  3. Matplotlib实例教程 | 统计DataFrame中文本长度分布(条形统计图)
  4. 搬运 centos7.2 apache 绑定二级目录 访问依然是apache页面
  5. web动画_Web动画简介
  6. javascript学习系列(18):数组中的include方法
  7. Silverlight 3.0 Isolated Storage 独立存储空间
  8. 字符串反转python_Python实现字符串反转的几种方法
  9. python工作环境创建_VirtualEnv 和Pip 构建Python的虚拟工作环境
  10. linux ppp 串口 gprs,linux下GPRS ppp拨号默认路由问题(存在eth0)
  11. java查询出来的日期类型_Java的第29天,Oracle函数
  12. Trick(四)——翻转字符串的实现
  13. 使用Adobe acrobat压缩pdf大小
  14. SharePoint 2013 Error - File names can't contain the following characters: ? # {} % ~ / \.
  15. Erueka状态变更说明(十三)
  16. 线性代数-矩阵方程应用:配平化学方程式
  17. 智商、情商和逆商与程序员职业生涯发展
  18. 1588 1-Step 和 2-Step PTP 之间有什么区别?
  19. vivo S16/S15/S12/S10 PRO卡刷线刷系统升级降级推荐成功解决屏幕锁不记得开机锁成功刷好的有效方案
  20. FastDfs与ElasticSearch和Mysql完成海量数据存储搜索功能

热门文章

  1. .NET CORE迁移踩坑
  2. Cutting Sticks UVA - 10003
  3. git 代码托管使用方法
  4. 转Python 标准库 urllib2 的使用细节
  5. android数据存储方式(三)----Files
  6. MYSQL数据库从A表把数据插入B表
  7. java连接cassandra,使用JDBC驱动程序连接到Cassandra
  8. .net core vs java_VS Code 1.19.3调试.net core 2并始终满足“只能调试64位进程”
  9. android 调用js怎么获取返回值_js 函数
  10. 计算机组成原理整数乘法,计算机组成原理 - 定点整数的原码补码运算(待验证)...