例:
组件与组件之间的传递代替Intent,线程间的通讯代替handler
加依赖:
dependencies { compile 'org.greenrobot:eventbus:3.0.0' }
加权限:
MainActivity
/** * Module,EventBus,2Activity * <p> * Module * <p> * EventBus,EventBus * <p> * EventBus, * <p> * EventBusPost * <p> * , * * Intent,handler */ public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button button; private TextView tv_title; private EventBus mEventBus; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); //EventBUsnewEventBUs, mEventBus = EventBus.getDefault(); //EventBus,:EventBus,:1.娉ㄨВ 2. mEventBus.register(this); } @Override protected void onDestroy() { super.onDestroy(); //EventBUs mEventBus.unregister(this); } /** , * //:,,public, * :: * ThreadMode.MAIN * , */ @Subscribe(threadMode = ThreadMode.MAIN) public void eBSAEventBus(EBMessage ebMessage){ // tv_title.setText(ebMessage.Message); Log.d("EventBusThread","MAIN"+Thread.currentThread().getName()); } // @Subscribe(threadMode = ThreadMode.POSTING) // public void eBSAEventBus1(EBMessage ebMessage){ // Log.d("EventBusThread","POSTING"+Thread.currentThread().getName()); // } // // @Subscribe(threadMode = ThreadMode.ASYNC) // public void eBSAEventBus2(EBMessage ebMessage){ // Log.d("EventBusThread","ASYNC"+Thread.currentThread().getName()); // } // // @Subscribe(threadMode = ThreadMode.BACKGROUND) // public void eBSAEventBus3(EBMessage ebMessage){ // Log.d("EventBusThread","BACKGROUND"+Thread.currentThread().getName()); // } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: // startActivity(new Intent(this,EventBusSendActivity.class)); new Thread(){ public void run() { mEventBus.post(new EBMessage(",")); }; }.start(); break; } }
activity_main
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button" android:text="Activity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true"/> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button" android:layout_centerHorizontal="true" android:layout_marginTop="35dp"/> </RelativeLayout>
/**
public class EBMessage { public String Message; public EBMessage(String message) { Message = message; } }
EventBusSendActivity
public class EventBusSendActivity extends AppCompatActivity { String mString = "1601Rnb,"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_event_bus_send); } public void send(View view){ //4.,, EventBus.getDefault().post(new EBMessage(mString)); finish(); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_event_bus_send" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:onClick="send" android:text="EventBus" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout>
文章来源: EventBus的Post方法发送事件