接上篇,學習如何使用EventBus。
如果是用Android Studio開發的話,在Gradle文件中
compile 'org.simple:androideventbus:1.0.5'
即可使用了。下面貼代碼:
public class SubscriberActivity extends AppCompatActivity { public static final String GET_MESSAGE_SUCCESS ="get_message_success" ; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_subscriber); if (isUseEventBus()){ EventBus.getDefault().register(this); } initView(); } protected boolean isUseEventBus(){ return true; } private TextView get_msg_tv; private void initView() { Button jump_btn = (Button) findViewById(R.id.jump_btn); jump_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(SubscriberActivity.this,RxJavaActivity.class); startActivity(intent); } }); get_msg_tv = (TextView) findViewById(R.id.get_msg_tv); } @Subscriber(tag = GET_MESSAGE_SUCCESS) private void getMessageSuccess(String msg){ get_msg_tv.setText(msg); } @Override protected void onDestroy() { super.onDestroy(); if (isUseEventBus()){ EventBus.getDefault().unregister(this); } } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/jump_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="jump to RxJavaActivity"/> <TextView android:id="@+id/get_msg_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="未收到msg的textView"/> </LinearLayout>
這是入口Activity。
public class RxJavaActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_rxjava); initView(); } private void initView() { final String msg = "由RxJavaActivity發送給SubscriberActivity的消息"; Button send_msg_btn = (Button) findViewById(R.id.send_msg_btn); send_msg_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EventBus.getDefault().post(msg,SubscriberActivity.GET_MESSAGE_SUCCESS); } }); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/send_msg_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="send message to activity first"/> </LinearLayout>
這是返回msg的Activity。
EventBus使用就是這么簡單的一句
EventBus.getDefault().post(msg,SubscriberActivity.GET_MESSAGE_SUCCESS);
參數一是你需要傳遞過去的數據data,參數二是tag,自定義一個常量字符串即可。
這是發送,那怎么接收呢
@Subscriber(tag = GET_MESSAGE_SUCCESS) private void getMessageSuccess(String msg){ get_msg_tv.setText(msg); }
注解Subscriber,tag=上面自定義的字符串常量,方法名自定義(其實沒什么作用),而EventBus識別具體從哪個EventBus源(一個項目中肯定有多出Eventbus的post)post過來的通過①tag②方法的參數。
當他檢查到tag一致,參數(post方法的第一個參數與自定義方法的參數一致)一致,即能響應。
當然最關鍵的一步:注冊。
EventBus.getDefault().register(this);
這一步必不可少,否則無效。而上面貼的代碼里我們自定義了一個方法 isUseEventBus ,當我們開發一個項目的時候必然有一個基類,直接將注冊寫在基類中,通過這個方法決定你的子類要不要使用EventBus即可。
最后別忘了在 onDestroy 方法中注銷EventBus,這是個好習慣(涉及到內存泄漏問題)。
到此我們就會發現不需要你自定義接口,不需要用Handler處理子線程主線程等繁瑣的問題,確實比接口回調省事多了。雖然栗子很簡單,但是在實際項目中有更大的使用價值。