这个和普通的事件总线的发送接收一样。
-
package com.example.mysimpleeventbus; import java.util.ArrayList;
import java.util.List; import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;
import org.simple.eventbus.ThreadMode; import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends ActionBarActivity implements OnClickListener { private Button button;
private Button ansyBtn;
private Button ansy1Btn;
private Button sendListBtn; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
// 1 首先注册事件总线
EventBus.getDefault().register(this);
} private void initView() {
button = (Button) findViewById(R.id.button);
ansyBtn = (Button) findViewById(R.id.ansyBtn);
ansy1Btn = (Button) findViewById(R.id.ansy1Btn);
sendListBtn = (Button) findViewById(R.id.sendListBtn); button.setOnClickListener(this);
ansyBtn.setOnClickListener(this);
ansy1Btn.setOnClickListener(this);
sendListBtn.setOnClickListener(this);
} @Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
} @Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button: postSticky(); break; case R.id.ansyBtn: postAnsy(); break;
case R.id.ansy1Btn: postAnsyInMainActivity(); break;
case R.id.sendListBtn: // postListDate();
postListToOtherActivity();
break;
default:
break;
}
} private void postListToOtherActivity() {
List<User> users = new ArrayList<User>();
User user;
for (int i = 0; i < 10; i++) {
user = new User("帅哥" + i, "清华大学");
users.add(user);
} EventBus.getDefault().postSticky(users);
gotoOtherActivity();
} /**
95 * List数据的传递
96 */
97 private void postListDate() {
98 List<User> users = new ArrayList<User>();
99 User user;
100 for (int i = 0; i < 10; i++) {
101 user = new User("帅哥" + i, "清华大学");
102 users.add(user);
103 }
104
105 EventBus.getDefault().post(users);
106 }
@Subscriber
109 private void reveiveList(List<User> users) {
110 for (int i = 0; i < users.size(); i++) {
111 Toast.makeText(getApplicationContext(), users.get(i).toString(), Toast.LENGTH_SHORT).show();
112 }
113 }
/**
* Ansy事件传递,多个Activity之间必须postSticky
*/
private void postAnsy() {
// 将目标函数执行在异步线程中
EventBus.getDefault().postSticky(new User("soyoungboy", "西安财经学院"),
"ansy");
gotoOtherActivity();
} /**
* 在本Activity中进行Ansy事件发布操作
*/
private void postAnsyInMainActivity() {
EventBus.getDefault().post(new User("soyoungboy", "西安财经学院"), "ansy");
} /**
* 在本Activity中进行Ansy事件接受操作
*
* @param user
*/
@Subscriber(tag = "ansy", mode = ThreadMode.ASYNC)
private void ansy(User user) {
Log.i("user", user.toString());
Log.i("ThreadName", Thread.currentThread().getName());
} private void postSticky() {
// 2 发送Sticky事件
EventBus.getDefault().postSticky(new User("soyoungboy", "西安财经学院"),
"soyoungboy"); gotoOtherActivity();
} private void gotoOtherActivity() {
// 跳转页面
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
}
当然也可以在不同Activity之间进行事件总线的传递,不过要使用postSticky
发送:
-
private void postListToOtherActivity() {
List<User> users = new ArrayList<User>();
User user;
for (int i = 0; i < 10; i++) {
user = new User("帅哥" + i, "清华大学");
users.add(user);
} EventBus.getDefault().postSticky(users);
gotoOtherActivity();
}
另外个Activity中:
接收:
//获取另外一个Activity的list数据
@Subscriber
private void reveiveList(List<User> users) {
for (int i = 0; i < users.size(); i++) {
Toast.makeText(getApplicationContext(), users.get(i).toString(),
Toast.LENGTH_SHORT).show();
}
// 删除事件,否则会出现重复的响应事件
EventBus.getDefault().removeStickyEvent(users.getClass());
}
,当然可以通过tag的方式,来对事件进行过滤(无论在当前Activity还是跳转到其他Activity中接收的时候进行过滤),注意两边一定要一致,发送没tag,接收就别弄tag相关,重要的事情只说一遍,还有就是一个Acitivity内的无需删除的操作,而多个则必须加上EventBus.getDefault().removeStickyEvent(users.getClass())类似操作。