EventBus Otto and third-party frameworks

Day 11 EventBus Otto and third-party frameworks
EventBus
a .EventBus description:
two .EventBus three elements of
three .EventBus four kinds of thread model
four cases of code:.
Otto third-party frameworks
EventBus
a .EventBus description:
EventBus between the components can be simplified communication, let us write simple code, which can effectively separate event sender and recipient (ie decoupling means).

Two .EventBus three elements
Event event. It can be any type.
Subscriber event subscribers.
Publisher Publisher of the event. We can publish any thread in the event, under normal circumstances, the use of EventBus.getDefault () can get a EventBus objects, and then call the post (Object) method can be.
Three .EventBus four kinds of threading model
POSTING (default) indicates that the thread event handler with the release event threads in the same thread.
MAIN means the thread event handler in the main thread (UI) thread, so there can not be time-consuming operation.
BACKGROUND event handler represents a thread in a background thread, so the UI can not operate. If you publish an event thread is the main thread (UI thread), then the event handler will open a background thread, if the fruit is publishing events thread a background thread, then the event handler on the use of the thread.
ASYNC said that no matter what the event is a publish thread, the event handler always creates a new child thread running, the same can not be UI operations.
Case four codes:
1, adding a dependency compile 'org.greenrobot: eventbus: 3.1.1'

2, registration EventBus - onCreate () method

3, unregister EventBus - onDestry () method

4, the subscriber explicitly, to pass the message parameter, and can specify the method is performed in the main thread

5, post news release

1. Add dependence:

Implementation 'org.greenrobot: EventBus: 3.0.0'
. 1
2. Event message class defined


public class MessageEvent{
    private String message;
    public  MessageEvent(String message){
        this.message=message;
    }
 
    public String getMessage() {
        return message;
    }
 
    public void setMessage(String message) {
        this.message = message;
    }
}

3. Registration and deregistration + Event + subscribers statement released
respectively Activity's onCreate () method and onDestory () method, the EventBus registration and deregistration.

Note: Method subscribers must be modified public

public class FirstActivity extends AppCompatActivity {
    private Button mButton;
    private TextView mText;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_activity);
        mButton = (Button) findViewById(R.id.btn1);
        mText = (TextView) findViewById(R.id.tv1);
        //注册
        EventBus.getDefault().register(this);
        //发布事件
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MessageEvent("欢迎大家浏览我写的博客"));
            }
        });
       
    }
	//声明订阅者
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(MessageEvent messageEvent) {
        mText.setText(messageEvent.getMessage());
    }
	//解除注册
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

Otto third-party frameworks
Note: Method subscribers must be modified public

1, introducing dependent implementation 'com.squareup: otto: 1.3.8'

2, define a class AppBus, inheritance Bus, ---- singleton, subclass object returns the Bus

3, registered in the main thread Bus
AppBus.getInstance () register (this). ;

4, unregister in onDestry () method

	@Override
	protected void onDestroy() {
		super.onDestroy();
		AppBus.getInstance().unregister(this);
	}

5, the statement Subscribers

	@Subscribe
	public void receiveMessage(String result)
	{
		Toast.makeText(this, "result =  " + result, Toast.LENGTH_SHORT).show();
	}

6, launched a subscription - the main thread

Guess you like

Origin blog.csdn.net/lizhuang_666/article/details/92847053