首先看一下官网上面的解析图
EventBus:Android的事件
EventBus是使用发布者/订阅者模式进行松散耦合的Android开源库。EventBus使得中央通信只需几行代码即可解耦类,简化代码,消除依赖关系,加快应用程序开发速度。
- 简化组件之间的通信
- 分离事件发送者和接收者
- 使用活动,片段和后台线程执行得很好
- 避免复杂和容易出错的依赖关系和生命周期问题
- 很快 专门针对高性能进行优化
- 很小(<50k瓶)
- 是在实践中被证明通过应用与100,000,000+安装
- 具有传送线程,用户优先级等高级功能。
官网上的代码
我自己呢就写了一个小小的demo,
首先看看效果图
点击main,跳转
点击发送,返回后
文字改变
接下来就是具体代码
首先就是依赖包
compile 'org.greenrobot:eventbus:3.0.0'
然后是activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.dell.eventbus.MainActivity"> <Button android:id="@+id/btn_try" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="main"/> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="啦啦"/> </LinearLayout>
还有activity_second.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.dell.eventbus.SecondActivity"> <Button android:id="@+id/btn_first_event" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送"/> </RelativeLayout>接下来就是main
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; /** * 作者 聂 */ public class MainActivity extends AppCompatActivity { private Button button; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //注册 EventBus.getDefault().register(this); button = (Button) findViewById(R.id.btn_try); tv = (TextView)findViewById(R.id.tv); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(MainActivity.this,SecondActivity.class); startActivity(intent); } }); } //定义处理接收的方法 @Subscribe(threadMode = ThreadMode.MAIN) public void onEvent(Event event){ String msg =event.getmMsg(); tv.setText(msg); } @Override protected void onDestroy() { super.onDestroy(); //反注册 EventBus.getDefault().unregister(this); } }
这里就是一个注册和一个反注册,还有就是一个按钮实现点击跳转到另一个Activity
下面就是另一个Activity
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import org.greenrobot.eventbus.EventBus; /** * 作者 聂 */ public class SecondActivity extends AppCompatActivity { private Button btn_FirstEvent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); btn_FirstEvent = (Button) findViewById(R.id.btn_first_event); btn_FirstEvent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //发送数据 EventBus.getDefault().post( new Event("聂雁宾帅")); } }); } }
而main里面的
//定义处理接收的方法 @Subscribe(threadMode = ThreadMode.MAIN) public void onEvent(Event event){ String msg =event.getmMsg(); tv.setText(msg); }就是来接收数据的
还有就是一个类,定义自己要传的值得类型
/** * Created by dell on 2017/10/27. */ public class Event { private String mMsg; public Event(String mMsg) { this.mMsg = mMsg; } public String getmMsg() { return mMsg; } public void setmMsg(String mMsg) { this.mMsg = mMsg; } }
我传的是String类型的
我就是EventBus的简单使用,
希望对你们有用