1.activity.main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_eventbus"
android:text="跳轉到下一頁"/>
<TextView
android:id="@+id/tv_eventBus"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
2.activity_second.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_eventbus_second"
android:text="獲取文本"/>
</LinearLayout>
3.MainActivity.java:
package company.com.eventbus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import de.greenrobot.event.EventBus;
public class MainActivity extends Activity implements View.OnClickListener {
private Button btn_eventBus;
private TextView tv_eventBus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
registeventBus();
addListener();
}
private void registeventBus() {
EventBus.getDefault().register(this);
}
private void addListener() {
btn_eventBus.setOnClickListener(this);
}
private void initView() {
btn_eventBus= (Button) findViewById(R.id.btn_eventbus);
tv_eventBus= (TextView) findViewById(R.id.tv_eventBus);
}
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
public void onEventMainThread(EventBusEntity eventBusEntity){
String msg = "onEventMainThread收到了消息:" + eventBusEntity.getMessage();
Log.d("harvic", msg);
tv_eventBus.setText(msg);
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
}
4.SecondActivity.java:
package company.com.eventbus;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import de.greenrobot.event.EventBus;
public class SecondActivity extends Activity implements View.OnClickListener{
private Button btn_eventBus_second;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
initView();
addListener();
}
private void initView() {
btn_eventBus_second= (Button) findViewById(R.id.btn_eventbus_second);
}
private void addListener() {
btn_eventBus_second.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.btn_eventbus_second){
EventBus.getDefault().post(new EventBusEntity("你好嗎?好久不見哦!"));
//EventBus.getDefault().post(new EventBusEntity());//無參,可將實體類傳過去
}
}
}
EventBus主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,線程之間傳遞消息.優點是開銷小,代碼更優雅。
EventBus進階:
EventBus還有另外有個不同的函數,他們分別是:
1、onEvent
2、onEventMainThread
3、onEventBackgroundThread
4、onEventAsync
onEvent:如果使用onEvent作爲訂閱函數,那麼該事件在哪個線程發佈出來的,onEvent就會在這個線程中運行,也就是說發佈事件和接收事件線程在同一個線程。使用這個方法時,在onEvent方法中不能執行耗時操作,如果執行耗時操作容易導致事件分發延遲。
onEventMainThread:如果使用onEventMainThread作爲訂閱函數,那麼不論事件是在哪個線程中發佈出來的,onEventMainThread都會在UI線程中執行,接收事件就會在UI線程中運行,這個在Android中是非常有用的,因爲在Android中只能在UI線程中跟新UI,所以在onEvnetMainThread方法中是不能執行耗時操作的。
onEventBackground:如果使用onEventBackgrond作爲訂閱函數,那麼如果事件是在UI線程中發佈出來的,那麼onEventBackground就會在子線程中運行,如果事件本來就是子線程中發佈出來的,那麼onEventBackground函數直接在該子線程中執行。
onEventAsync:使用這個函數作爲訂閱函數,那麼無論事件在哪個線程發佈,都會創建新的子線程在執行onEventAsync.
如果以上四個方法中接收到的實例是同一個,那麼四個方法都會執行,我們可以根據自己的需要選擇其中一種來使用,可以用上述同一種方法接收多個不同的實例,只要分開處理即可。例:
public void onEventMainThread(FirstEvent event) {
Log.d("harvic", "onEventMainThread收到了消息:" + event.getMsg());
}
public void onEventMainThread(SecondEvent event) {
Log.d("harvic", "onEventMainThread收到了消息:" + event.getMsg());
}
public void onEvent(ThirdEvent event) {
Log.d("harvic", "OnEvent收到了消息:" + event.getMsg());
}