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());
}