EventBus

Seeing the problems raised by everyone about Android, some of them can be solved with EventBus, and quite a few people recommend using EventsBus, because it and GreenDAO come from the same company, and it is very simple to use, so many Internet apps will use it now EventsBus for message passing.

Based on this, there are a lot of EventBus articles, which are very well written, but since EventBus has already released version 3.0, and most translations in China are only around version 2.4, for those who are new to EventBus, contact from the latest version Learning is the best way to learn.

So, here, I summarize the usage of EventBus3.0.

What is EventBus

EventBus is an Android-optimized publish/subscribe message bus, which simplifies the communication between components in the application and between components and background threads. For example, request the network, notify the UI through Handler or Broadcast when the network returns, and communicate between two Fragments through Listener. These requirements can be realized through EventBus .

EventBus framework

When people talk about EventBus, they always think of Greenrobot's EventBus, but in fact EventBus is a general name, such as Guava produced by Google, Guava is a huge library, and EventBus is just a small function attached to it, so it is used in actual projects. Not much. The most used is greenrobot/EventBus. The advantage of this library is that the interface is simple and the integration is convenient, but the method name is limited and annotations are not supported. Another library square/otto is modified from Guava and used by many people.

This blog post only discusses greenrobot's EventBus library for now.

Basic usage

Many articles will talk about Subscriber, as well as concepts such as Publisher and ThreadMode. I think it is unnecessary for the time being. It is simple and rude, and the code is directly written:

Add dependency library:

First you need to add dependencies to your app:

compile 'de.greenrobot:eventbus:3.0.0-beta1'

For how to add dependency libraries, please refer to Gradle for Android Part 3 (Dependency Management) .

Some people will ask why it is a beta version, because eventbus version 3.0 is only in beta testing stage at this stage. Some people will ask how to find the eventbus 3.0.0 version, specifically adding:

图片描述

register

For example, you need to register the eventbus event in an activity, and then define the receiving method, which is very similar to Android's broadcast mechanism. You need to register the broadcast first, and then you need to write an inner class to receive the broadcast, and then operate the UI, in the EventBus , you need to do the same.

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

    EventBus.getDefault().register(this);

}
@Override
protected void onDestroy() {
    super.onDestroy();
    EventBus.getDefault().unregister(this);
}

subscriber

Similar to broadcasting, but different from version 2.4, you don't have to agree to the beginning of the OnEvent method (it doesn't matter if you don't understand it):

@Subscribe(threadMode = ThreadMode.MainThread)
public void helloEventBus(String message){
    mText.setText(message);
}

This operation is very simple. It defines a hello method, which needs to pass in the String parameter, and operates the UI operation in it. Note:
we add the annotation @Subscribe, which means subscriber, and pass in threadMode, which we define as ThreadMode .MainThread, which means that the method is completed on the UI thread, so you don't have to worry about throwing exceptions. Is not it simple?

announcer

Now that you subscribe to something somewhere, of course you will publish a message somewhere. For example, your activity needs an http request, and the http request must be operated in an asynchronous thread. After returning the result, you can write:

String json="";
EventBus.getDefault().post(json);

This is OK, you can try to see if it works properly!

A preliminary study of the principle

You subscribed to the content, so you need to register EventBus in this class, and the method you subscribe needs to pass in String, that is, your received information is of type String, then when you post, what you post should also be of type String. message will be received.

If your post is an object

First you need to define a class like pojo:

public class MessageEvent {
  public final String name;
  public final String password;
  public MessageEvent(String name,String password) {
    this.name = name;
    this.password=password;
  }
}

Then when you post:

EventBus.getDefault().post(new MessageEvent("hello","world"));

Of course, the method you receive also needs to be changed to:

@Subscribe(threadMode = ThreadMode.MainThread)
public void helloEventBus(MessageEvent message){
    mText.setText(message.name);
}

Question, when you post a message, do you have multiple subscribers, does each one receive it? Can you specify the recipient.

下一章,带来源码解析以及EventBus的高级用法;
如果大家有兴趣,也可带领大家编写属于自己的EventBus框架,敬请期待。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324753535&siteId=291194637