【第三篇】学习 android 事件总线androidEventbus之发布事件,子线程中接收

时间:2024-06-11 21:04:14

发送和接收消息的方式类似其他的发送和接收消息的事件总线一样,不同的点或者应该注意的地方:

1,比如在子线程构造方法里面进行实现总线的注册操作;
2,要想子线程中接收消息的功能执行,必须启动线程。
3,添加tag和不添加tag类似其他。
 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;
private Button sendToThreadBtn; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
// 1 首先注册事件总线
EventBus.getDefault().register(this);
MyThread myThread = new MyThread();
myThread.start();
} 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);
sendToThreadBtn = (Button) findViewById(R.id.sendToThreadBtn); button.setOnClickListener(this);
ansyBtn.setOnClickListener(this);
ansy1Btn.setOnClickListener(this);
sendListBtn.setOnClickListener(this);
sendToThreadBtn.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;
case R.id.sendToThreadBtn: postToThread();

break;
default:
break;
}
} /**
93 * 发送事件总线到线程里面去接收
94 */
95 private void postToThread() {
96 EventBus.getDefault().post(new User("soyoungboy", "西安财经学院"),"thread");
97 }

class MyThread extends Thread{ public MyThread() {
EventBus.getDefault().register(this);
} /**
106 * 接收其他线程发来的消息
107 * @param user
108 */
109 @Subscriber(tag = "thread")
110 private void receiveUser(User user) {
111 Log.i("thread --thread == ", user.toString());
112 }
@Override
public void run() {
super.run();
}
} 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();
} /**
* List数据的传递
*/
private void postListDate() {
List<User> users = new ArrayList<User>();
User user;
for (int i = 0; i < 10; i++) {
user = new User("帅哥" + i, "清华大学");
users.add(user);
} EventBus.getDefault().post(users);
} @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();
}
} /**
* 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);
}
}