【第四篇】学习 android 事件总线androidEventbus之发布事件,子线程中接收
发送和接收消息的方式类似其他的发送和接收消息的事件总线一样,不同的点或者应该注意的地方:
1,比如在子线程构造方法里面进行实现总线的注册操作;
2,要想子线程中接收消息的功能执行,必须启动线程。
3,添加tag和不添加tag类似其他。
1 package com.example.mysimpleeventbus; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.simple.eventbus.EventBus; 7 import org.simple.eventbus.Subscriber; 8 import org.simple.eventbus.ThreadMode; 9 10 import android.content.Intent; 11 import android.os.Bundle; 12 import android.support.v7.app.ActionBarActivity; 13 import android.util.Log; 14 import android.view.View; 15 import android.view.View.OnClickListener; 16 import android.widget.Button; 17 import android.widget.Toast; 18 19 public class MainActivity extends ActionBarActivity implements OnClickListener { 20 21 private Button button; 22 private Button ansyBtn; 23 private Button ansy1Btn; 24 private Button sendListBtn; 25 private Button sendToThreadBtn; 26 27 @Override 28 protected void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.activity_main); 31 initView(); 32 // 1 首先注册事件总线 33 EventBus.getDefault().register(this); 34 MyThread myThread = new MyThread(); 35 myThread.start(); 36 } 37 38 private void initView() { 39 button = (Button) findViewById(R.id.button); 40 ansyBtn = (Button) findViewById(R.id.ansyBtn); 41 ansy1Btn = (Button) findViewById(R.id.ansy1Btn); 42 sendListBtn = (Button) findViewById(R.id.sendListBtn); 43 sendToThreadBtn = (Button) findViewById(R.id.sendToThreadBtn); 44 45 button.setOnClickListener(this); 46 ansyBtn.setOnClickListener(this); 47 ansy1Btn.setOnClickListener(this); 48 sendListBtn.setOnClickListener(this); 49 sendToThreadBtn.setOnClickListener(this); 50 } 51 52 @Override 53 protected void onDestroy() { 54 super.onDestroy(); 55 EventBus.getDefault().unregister(this); 56 } 57 58 @Override 59 public void onClick(View view) { 60 switch (view.getId()) { 61 case R.id.button: 62 63 postSticky(); 64 65 break; 66 67 case R.id.ansyBtn: 68 69 postAnsy(); 70 71 break; 72 case R.id.ansy1Btn: 73 74 postAnsyInMainActivity(); 75 76 break; 77 case R.id.sendListBtn: 78 79 // postListDate(); 80 postListToOtherActivity(); 81 break; 82 case R.id.sendToThreadBtn: 83 84 postToThread(); 85 86 break; 87 default: 88 break; 89 } 90 } 91 92 /** 93 * 发送事件总线到线程里面去接收 94 */ 95 private void postToThread() { 96 EventBus.getDefault().post(new User("soyoungboy", "西安财经学院"),"thread"); 97 } 98 99 class MyThread extends Thread{ 100 101 public MyThread() { 102 EventBus.getDefault().register(this); 103 } 104 105 /** 106 * 接收其他线程发来的消息 107 * @param user 108 */ 109 @Subscriber(tag = "thread") 110 private void receiveUser(User user) { 111 Log.i("thread --thread == ", user.toString()); 112 } 113 @Override 114 public void run() { 115 super.run(); 116 } 117 } 118 119 private void postListToOtherActivity() { 120 List<User> users = new ArrayList<User>(); 121 User user; 122 for (int i = 0; i < 10; i++) { 123 user = new User("帅哥" + i, "清华大学"); 124 users.add(user); 125 } 126 127 EventBus.getDefault().postSticky(users); 128 gotoOtherActivity(); 129 } 130 131 /** 132 * List数据的传递 133 */ 134 private void postListDate() { 135 List<User> users = new ArrayList<User>(); 136 User user; 137 for (int i = 0; i < 10; i++) { 138 user = new User("帅哥" + i, "清华大学"); 139 users.add(user); 140 } 141 142 EventBus.getDefault().post(users); 143 } 144 145 @Subscriber 146 private void reveiveList(List<User> users) { 147 for (int i = 0; i < users.size(); i++) { 148 Toast.makeText(getApplicationContext(), users.get(i).toString(), 149 Toast.LENGTH_SHORT).show(); 150 } 151 } 152 153 /** 154 * Ansy事件传递,多个Activity之间必须postSticky 155 */ 156 private void postAnsy() { 157 // 将目标函数执行在异步线程中 158 EventBus.getDefault().postSticky(new User("soyoungboy", "西安财经学院"), 159 "ansy"); 160 gotoOtherActivity(); 161 } 162 163 /** 164 * 在本Activity中进行Ansy事件发布操作 165 */ 166 private void postAnsyInMainActivity() { 167 EventBus.getDefault().post(new User("soyoungboy", "西安财经学院"), "ansy"); 168 } 169 170 /** 171 * 在本Activity中进行Ansy事件接受操作 172 * 173 * @param user 174 */ 175 @Subscriber(tag = "ansy", mode = ThreadMode.ASYNC) 176 private void ansy(User user) { 177 Log.i("user", user.toString()); 178 Log.i("ThreadName", Thread.currentThread().getName()); 179 } 180 181 private void postSticky() { 182 // 2 发送Sticky事件 183 EventBus.getDefault().postSticky(new User("soyoungboy", "西安财经学院"), 184 "soyoungboy"); 185 186 gotoOtherActivity(); 187 } 188 189 private void gotoOtherActivity() { 190 // 跳转页面 191 Intent intent = new Intent(MainActivity.this, OtherActivity.class); 192 startActivity(intent); 193 } 194 }