EventBus 3.1.1 源碼解析(二)

EventBus從3.0開始支持APT,編譯期生成文件,就可以得到eventType、subscriberMethod、subscriberClass的映射關係。

app下的build.gradle中引入EventBus和EventBus註解處理器

android {
    defaultConfig {
    
 		//添加這個,eventBusIndex 
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [ eventBusIndex : 'packageName.MyEventBusIndex' ]
            }
        }
        
    } 
}

dependencies {
//添加eventBus、eventBus的註解
    implementation 'org.greenrobot:eventbus:3.1.1'
    annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.0.1'
}

RetrofitActivity.java

  • 在onCreate()/onDestroy中分別註冊、取消註冊eventBus
  • 定義兩個方法,監聽同一個事件Repo
  • 定義一個方法,監聽Book事件
	EventBus mEventBus = EventBus.builder().addIndex(new MyEventBusIndex()).build();

 	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mEventBus.register(this); //註冊
    }

 	@Subscribe
    public void receiveRepo(Repo repo) {} //監聽Repo 1

    @Subscribe
    public void receiveRepoTwo(Repo repo) {} //監聽Repo 2

	@Subscribe
    public void receiveBook(Book book) {} //監聽Book

	@Override
    protected void onDestroy() {
        mEventBus.unregister(this); //取消註冊
        super.onDestroy();
    }

拷貝一份RetrofitActivity.java ,並重命名爲RetrofitTwoActivity.java,build項目

在app/build/generated/source/apt/debug/packageName下,會自動生成一個MyEventBusIndex.java文件。

如下:

public class MyEventBusIndex implements SubscriberInfoIndex {
    private static final Map<Class<?>, SubscriberInfo> SUBSCRIBER_INDEX;

    static { //靜態語句
        SUBSCRIBER_INDEX = new HashMap<Class<?>, SubscriberInfo>();

        putIndex(new SimpleSubscriberInfo(com.example.dexloader.net.RetrofitActivity.class, 
                true, new SubscriberMethodInfo[] {
            new SubscriberMethodInfo("receiveRepo", com.example.dexloader.net.Repo.class),
            new SubscriberMethodInfo("receiveRepoTwo", com.example.dexloader.net.Repo.class),
            new SubscriberMethodInfo("receiveBook", com.example.dexloader.gson.Book.class),
        }));

        putIndex(new SimpleSubscriberInfo(com.example.dexloader.net.RetrofitTwoActivity.class, 
                true, new SubscriberMethodInfo[] {
            new SubscriberMethodInfo("receiveRepo", com.example.dexloader.net.Repo.class),
            new SubscriberMethodInfo("receiveRepoTwo", com.example.dexloader.net.Repo.class),
            new SubscriberMethodInfo("receiveBook", com.example.dexloader.gson.Book.class),
        }));

    }

    private static void putIndex(SubscriberInfo info) {
        SUBSCRIBER_INDEX.put(info.getSubscriberClass(), info);
    }

    @Override
    public SubscriberInfo getSubscriberInfo(Class<?> subscriberClass) {
        SubscriberInfo info = SUBSCRIBER_INDEX.get(subscriberClass);
        if (info != null) {
            return info;
        } else {
            return null;
        }
    }
}

參考:
Android註解使用之註解編譯android-apt如何切換到annotationProcessor

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章