Android学习之EventBus基础篇

一、概述

EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间传递消息.优点是开销小,代码更优雅。以及将发送者和接收者解耦。
Github地址:https://github.com/greenrobot/EventBus

二、实例演示

Android学习之EventBus基础篇_第1张图片
点击MainActivity页面里的跳转按钮进入SecondActivity页面,点击发送EventBus事件按钮,进行事件消息发送操作,我们在返回到MainActivity页面发现,我们成功的接收到SecondActivity页面发送过来的消息。

1.添加依赖:

compile 'org.greenrobot:eventbus:3.0.0'

2.MainActivity.java

package com.czhappy.eventbusdemo.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.czhappy.eventbusdemo.R;
import com.czhappy.eventbusdemo.model.MessageEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends AppCompatActivity {

    private TextView receive_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //注册eventBus
        EventBus.getDefault().register(this);
        receive_tv = (TextView) this.findViewById(R.id.receive_tv);
    }

    public void goSecondBtn(View view){
        SecondActivity.startActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消注册
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(MessageEvent event) {
        if (!TextUtils.isEmpty(event.getMsg())) {
            receive_tv.setText("接收到消息:" + event.getMsg());
        }
    }
}

3.activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="跳转"
        android:onClick="goSecondBtn"/>

    <TextView
        android:id="@+id/receive_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textColor="#333333"
        android:textSize="16sp"/>
LinearLayout>

4.SecondActivity.java

package com.czhappy.eventbusdemo.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.czhappy.eventbusdemo.R;
import com.czhappy.eventbusdemo.model.MessageEvent;

import org.greenrobot.eventbus.EventBus;

/**
 * author: chenzheng
 * created on: 2017/8/2 8:08
 * description:
 */

public class SecondActivity extends AppCompatActivity {

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

    public void sendEventBtn(View view){
        EventBus.getDefault().post(new MessageEvent("大家好,我是小明!"));
    }

    public static void startActivity(Context context){
        Intent intent = new Intent(context, SecondActivity.class);
        context.startActivity(intent);
    }

}

5.activity_second.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="发送EventBus事件"
        android:onClick="sendEventBtn"/>

LinearLayout>

你可能感兴趣的:(Android进阶)