本文共 1473 字,大约阅读时间需要 4 分钟。
import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;import org.greenrobot.eventbus.ThreadMode;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EventBus.getDefault().register(this); MyEvent event = new MyEvent(); event.id = 1; event.time = System.currentTimeMillis(); EventBus.getDefault().post(event); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } @Subscribe(threadMode = ThreadMode.MAIN, priority = 8) public void onMessageEvent1(MyEvent event) { Log.d("MAIN消息1", event.id + "," + event.time); } @Subscribe(threadMode = ThreadMode.MAIN, priority = 9) public void onMessageEvent2(MyEvent event) { Log.d("MAIN消息2", event.id + "," + event.time); } @Subscribe(threadMode = ThreadMode.BACKGROUND, priority = 98) public void onMessageEvent3(MyEvent event) { Log.d("BACKGROUND消息3", event.id + "," + event.time); } @Subscribe(threadMode = ThreadMode.BACKGROUND, priority = 99) public void onMessageEvent4(MyEvent event) { Log.d("BACKGROUND消息4", event.id + "," + event.time); } private class MyEvent { public int id; public long time; }}
转载地址:http://nnvwo.baihongyu.com/