EventBus通信

时间:2021-12-06 08:48:27

需求:

1.ActivityA打开ActivityB

2.在B中执行某操作后,同时执行A中的方法

lib下载:eventbus-2.4.0.jar  jmmy

1.在EventBusTestActivity注册eventBus

EventBus.getDefault().register(this);// 注册广播

2.点击事件跳转

Intent intent = new Intent(getApplicationContext(),EventBusTest2Activity.class);
startActivity(intent);

3.在EventBusTest2Activity中发送广播

EventBus.getDefault().post(new MyEvent("Message From EventBusTest2"));

4.EventBusTestActivity接收广播

// onEvent会放在发送事件的那个线程中去执行,不能进行UI更新
public void onEvent(MyEvent event) {
Log.i("What", "[onEvent]My Thread is "
+ Thread.currentThread().getName());
} // onEventMainThread就是会放到主线程去执行的事件处理,一般在其中进行比如UI的更新的操作
public void onEventMainThread(MyEvent event) {
Log.i("What", "[onEventMainThread] Thread is "
+ Thread.currentThread().getName());
test1.setText("测试完成");
}

5.解除广播

protected void onDestroy() {
  super.onDestroy();
  EventBus.getDefault().unregister(this);// 解除
}

下面贴上源码:

package com.example.activity;

import com.example.event.MyEvent;

import de.greenrobot.event.EventBus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class EventBusTestActivity extends Activity implements OnClickListener {
private Button test1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_bus_test);
test1 = (Button) findViewById(R.id.test1);
test1.setOnClickListener(this);
EventBus.getDefault().register(this);// 注册广播
} // onEvent会放在发送事件的那个线程中去执行,不能进行UI更新
public void onEvent(MyEvent event) {
Log.i("What", "[onEvent]My Thread is "
+ Thread.currentThread().getName());
} // onEventMainThread就是会放到主线程去执行的事件处理,一般在其中进行比如UI的更新的操作
public void onEventMainThread(MyEvent event) {
Log.i("What", "[onEventMainThread] Thread is "
+ Thread.currentThread().getName());
test1.setText("测试完成");
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.test1:
Intent intent = new Intent(getApplicationContext(),
EventBusTest2Activity.class);
startActivity(intent);
break;
}
} @Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);// 解除
}
}
package com.example.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; import com.example.event.MyEvent; import de.greenrobot.event.EventBus; public class EventBusTest2Activity extends Activity {
private Button click1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_bus_test2); click1 = (Button) findViewById(R.id.click1);
click1.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
EventBus.getDefault().post(
new MyEvent("Message From EventBusTest2"));
}
});
}
}