标签:textview www 解耦 发布 pass exception protected 左右 file
一、写在前面
还在为时间接收而烦恼吗?还在为各种组件间的消息传递烦恼吗?EventBus 3.0,专注于android的发布、订阅事件总线,让各组件间的消息传递更简单!完美替代Intent,Handler,BroadCast,接口等传统方案。这么优雅的功能实现,却只有50k左右的jar包,无疑集快、小、优雅解耦于一身,目前最新版本为3.0,与之前版本有所冲突,官方网址:https://github.com/greenrobot/EventBus
二、使用简介
总的流程只需要4步:添加依赖=>编写事件类=>发送事件=>接收事件并处理。
通过一个例子说明:例子为三个Activity,入口Activity点击一个Button跳转到第二个Activity,第二个Activity点击跳转第三个,第三个可以发送数据让第一个Activity接收,具体可看下面的gif图,代码详见github:https://github.com/nanchen2251/EventbusDemo
1、添加依赖
1 compile ‘org.greenrobot:eventbus:3.0.0‘
2、编写事件类,这里可以传递任意类型,先来传递一个Object类吧
1 package com.nanchen.ischool.eventbusdemo; 2 3 /** 4 * @author nanchen 5 * @fileName EventBusDemo 6 * @packageName com.nanchen.ischool.eventbusdemo 7 * @date 2016/11/22 15:42 8 */ 9 10 public class MessageEvent { 11 private User mUser; 12 13 public MessageEvent(User user) { 14 mUser = user; 15 } 16 17 public User getUser() { 18 return mUser; 19 } 20 21 public void setUser(User user) { 22 mUser = user; 23 } 24 }
User类很简单
1 package com.nanchen.ischool.eventbusdemo; 2 3 /** 4 * @author nanchen 5 * @fileName EventBusDemo 6 * @packageName com.nanchen.ischool.eventbusdemo 7 * @date 2016/11/22 15:42 8 */ 9 10 public class User { 11 private String username; 12 private String password; 13 14 public User(String username, String password) { 15 this.username = username; 16 this.password = password; 17 } 18 19 public String getUsername() { 20 return username; 21 } 22 23 public void setUsername(String username) { 24 this.username = username; 25 } 26 27 public String getPassword() { 28 return password; 29 } 30 31 public void setPassword(String password) { 32 this.password = password; 33 } 34 }
3、post发送事件(这里我是使用三个Activity,布局只有简单的Button和TextView,具体代码可上github查看)
1 package com.nanchen.ischool.eventbusdemo; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.EditText; 7 8 import org.greenrobot.eventbus.EventBus; 9 10 public class Main3Activity extends AppCompatActivity { 11 12 private EditText mEditUsername; 13 private EditText mEditPwd; 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main3); 19 mEditUsername = (EditText) findViewById(R.id.edit_username); 20 mEditPwd = (EditText) findViewById(R.id.edit_pwd); 21 } 22 23 public void btnClick3(View view) { 24 String username = mEditUsername.getText().toString().trim(); 25 String password = mEditPwd.getText().toString().trim(); 26 EventBus.getDefault().post(new MessageEvent(new User(username,password))); 27 finish(); 28 } 29 }
4、订阅者接收事件
1 package com.nanchen.ischool.eventbusdemo; 2 3 import android.content.Intent; 4 import android.os.Bundle; 5 import android.support.v7.app.AppCompatActivity; 6 import android.view.View; 7 import android.widget.TextView; 8 9 import org.greenrobot.eventbus.EventBus; 10 import org.greenrobot.eventbus.Subscribe; 11 import org.greenrobot.eventbus.ThreadMode; 12 13 public class MainActivity extends AppCompatActivity { 14 15 private TextView mTextView; 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 22 mTextView = (TextView) findViewById(R.id.text); 23 24 if (!EventBus.getDefault().isRegistered(this)){ 25 EventBus.getDefault().register(this); 26 } 27 } 28 29 public void btnClick(View view) { 30 startActivity(new Intent(this,Main2Activity.class)); 31 } 32 33 @Subscribe(threadMode = ThreadMode.MAIN) 34 public void onEventMainThread(MessageEvent event) { 35 if (event.getUser() != null){ 36 mTextView.setText("用户名:"+event.getUser().getUsername() 37 +",密码:"+event.getUser().getPassword()); 38 } 39 } 40 41 42 @Override 43 protected void onDestroy() { 44 super.onDestroy(); 45 EventBus.getDefault().unregister(this); 46 } 47 }
三、注意事项
1、注册一般写在onCreate或者onStart中,尽量不要写在onResume,可能出现多次注册,比如:
EventBusException: Subscriber class already registered to event class
2、取消注册一定写在onDestory,写在onStop可能会引发异常;
3、建议注册前先判断一下是否已经注册,避免重复注册,并且注册尽量放在各变量初始化最后,以免造成空指针异常;
1 if (!EventBus.getDefault().isRegistered(this)) { 2 EventBus.getDefault().register(this); 3 }
4、3.0和之前版本不兼容,现在采用注解的方法来接收事件,四种注解方式分别为:
1) @Subscrible 对应之前的onEvent();
2) @Subscrible(threadMode = ThreadMode.ASYNC) 对应之前的onEventAsync();
3) @Subscrible(threadMode = ThreadMode.BACKGRAND) 对应之前的onEventBackground();
4) @Subscrible(threadMode = ThreadMode.MAIN) 对应之前的onEventMainThread();
5、采用注解后方法名没有限制了,参数只有一个,和发送者post的参数对应配对,未声明threadMode的时候默认线程模式是ThreadMode.POSTING,只有在该模式下才可以取消线程,否则容易导致如下异常:org.greenrobot.eventbus.EventBusException: This method may only be called from inside event handling methods on the posting thread
四、写在最后
有不理解的请在评论区留言,博主一向都是直播模式对待每一篇博客,如果你喜欢,点个推荐吧~转载请附带本文链接:http://www.cnblogs.com/liushilin/p/6089785.html
【热门技术】EventBus 3.0,让事件订阅更简单,从此告别组件消息传递烦恼~
标签:textview www 解耦 发布 pass exception protected 左右 file
原文地址:http://www.cnblogs.com/liushilin/p/6089785.html