The use of Android EventBus in Activity, Fragment and Service

EventBus is an open source project of Android. Its most important function is to simplify the message passing between various components, similar to Android's own broadcast. But it is much more convenient to use than broadcasting.

github地址:https://github.com/greenrobot/EventBus
官方文档:http://greenrobot.org/eventbus/documentation

1. In Android studio

Add in build.gradle

compile 'de.greenrobot:eventbus:2.4.0'

or in eclipse

Import the EventBus jar package and copy the jar directly to the libs folder in the project directory.

2. Define a MessageEvent class

This class is an ordinary Object, nothing special. We use it as the carrier of message passing.

    public class MessageEvent {  
        public final String message;  

        public MessageEvent(String message) {  
            this.message = message;  
        }  
    }  

3. Register EventBus.

Register EventBus in the Activity, Fragmen or Service to receive messages.
We can register in onCreate. Next, register in onCreat in Activity. The method is as follows

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //进行EventBus的注册
        EventBus.getDefault().register(this); 
}

4. Send a message.

Sending a message is done by using the Post method in EventBus in the Activity, Fragmen or Service where the message is to be sent. What is sent is an instance of our newly created class!

EventBus.getDefault().post(new MessageEvent("发送消息"));

5. Receive messages.

In the Activity, Fragmen or Service that wants to receive the message, override the onEvent method in the framework. There are four methods. You can override them as needed. Generally, you only need to define the first one below.

(1) We only need to define this method. It is not an interface or anything, and does not require any other operations. The thread in which this method is executed is determined by the thread of the post method. The thread onEvent is executed in which thread it is executed. The method is as follows:

public void onEvent(MessageEvent event){  
    Toast.makeText(getActivity(),event.message,
    Toast.LENGTH_SHORT).show();  
}  

(2) If we want to save the message when we receive it now, then we can directly define a method as follows:

public void onEventBackgroundThread(MessageEvent event){  
    saveToDisk(event.message);  

(3) The following method will be executed in a sub-thread outside the main thread. If the post method itself is a sub-thread, it will be executed in the same thread as the post method. It should be noted that when there are multiple posts, it will not Actively opening new threads will only be executed one by one in order, so we can ensure that our messages are saved in order. So what if we want to open a new thread for execution every time we receive a message? Just define the following method:

public void onEventAsync(MessageEvent event){  
    backend.send(event.message);  

(4) The following method is executed in threads that are different from the post thread and the main thread. A new thread will be opened for each post. When using this method, you should consider the number of times the method is triggered. Do not allow too many threads to cause excessive resource consumption. Finally, the most commonly used one, if we want to update the UI when receiving a message, because the UI can only be updated on the main thread, so we want the method to receive the message to be executed on the main thread, then we can directly define a method as follows .

public void onEventMainThread(MessageEvent event) {  
    textField.setText(event.message);  
}  

6. Log out of EventBus.

Log out in the Activity, Fragmen or Service to receive the message, and override the onDestroy() method

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

———————————————
转载于:https://blog.csdn.net/a373595475/article/details/78369817

Guess you like

Origin blog.csdn.net/weixin_42602900/article/details/133811522