Android EventBus技能点梳理
EventBus为Github上的开源项目,地址:https://github.com/greenrobot/EventBus
疑问:1. 现在都是Android Studio创建的项目,如何导入这些项目(对工具不熟悉);2. 如何得到这些开源项目的.jar包?
EventBus概念分析(获取感性认识):

所述publisher为发布者,subscriber为订阅者;Event的Publisher为事件的所有者,而各个Subscriber会收到对应的Event。
其主要功能是替代Intent、Handler、BroadCast在Fragment、Activity、Service以及线程之间传递消息。
优点在于:开销小(jar包小~50KB)、代码优雅、代码运行高效、经过多个APK测试...
EventBus使用基本流程:
实例分析:在EventBusActivity中点击按键,跳转到ActivitySecond界面;并在上述界面中点击按键,跳转到ActivityThird界面;并在上述界面中输入用户名和密码,点击跳转,返回到EventBusActivity中并显示传递过来的用户名和密码。
package com.demo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import de.greenrobot.event.EventBus;
public class EventBusActivity extends Activity {
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.text);
// 判断是否已经注册了,避免重复注册
if (!EventBus.getDefault().isRegistered(this)) {
// 注册监听,尽量写在各变量初始化之后,避免造成空指针异常
EventBus.getDefault().register(this);
}
}
public void btnClick(View view) {
startActivity(new Intent(EventBusActivity.this, ActivitySecond.class));
}
/**
* <功能描述> 订阅者接收事件
*
* @param event [参数说明]
* @return void [返回类型说明]
*/
public void onEventMainThread(MessageEvent event) {
if (event.getUser() != null) {
mTextView.setText("用户名:" + event.getUser().getUsername() + "; 密码:"
+ event.getUser().getPassword());
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// 取消注册需要写在onDestory(),写在onStop()中可能会引发异常
EventBus.getDefault().unregister(this);
}
}
ActivitySecond.java代码实例:
package com.demo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class ActivitySecond extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void btnClick2(View view) {
startActivity(new Intent(ActivitySecond.this, ActivityThird.class));
finish();
}
}
ActivityThird.java代码实例:
package com.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import de.greenrobot.event.EventBus;
public class ActivityThird extends Activity {
private EditText mEditUsername;
private EditText mEditPwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
mEditUsername = (EditText) findViewById(R.id.edit_username);
mEditPwd = (EditText) findViewById(R.id.edit_pwd);
}
public void btnClick3(View view) {
String userName = mEditUsername.getText().toString().trim();
String password = mEditPwd.getText().toString().trim();
// 发送消息
EventBus.getDefault().post(
new MessageEvent(new User(userName, password)));
finish();
}
}
EventBus注意事项:
接收消息时涉及到的四个方法,以及区分:
1. onEvent():该事件在哪个线程发布的,onEvent()就会在这个线程中运行。也就是说,发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent()中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。
2. onEventMainThread():不论事件在哪个线程中发布出来,onEventMainThread都会在UI线程中执行,接收事件都会在UI线程中运行。这种情况对于Android是非常有用的,因为在Android中只能在UI线程中更新UI,所以不能在该方法中执行耗时操作。
3. onEventBackground():如果事件是从UI线程中被发布出来,那么onEventBackground就会在创建的子线程中运行,如果事件本身就是在子线程中发布出来,那么就直接在该子线程中运行。、
4. onEventAsync():不论事件是在哪个线程中被发布出来,都会创建新的子线程中执行该方法。
如果有多个地方发布事件,有多个地方接收并处理事件,如何进行匹配和判断?
根据上述4种接收事件的方法中涉及到的参数,并进行参数匹配;发送消息的参数是某一个类,接收的也必须是这个类,否则接收不到。如果有多个onEvent()的参数相同,则这些方法都将接收到这个消息。
Android EventBus技能点梳理的更多相关文章
- android EventBus的简单使用
今天,简单讲讲Android里关于EventBus的使用. 这几天,由于面试的缘故,我听到了很多Android的流行框架,但是之前自己在公司做APP时并没有使用,所以没有了解.于是在网上查找了资料,学 ...
- Android EventBus实战 没听过你就out了
转载请表明出处:http://blog.csdn.net/lmj623565791/article/details/40794879,本文出自:[张鸿洋的博客] 1.概述 最近大家面试说经常被问到Ev ...
- Android EventBus源码解析 带你深入理解EventBus
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40920453,本文出自:[张鸿洋的博客] 上一篇带大家初步了解了EventBus ...
- android EventBus 的使用
今天简单的介绍 一下啊 android EventBus 的使用 EventBus 在官方介绍中是订阅......什么的 一大堆 , 在我android 菜鸟眼里 就是用来代替android 广 ...
- Android EventBus源代码解析 带你深入理解EventBus
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40920453,本文出自:[张鸿洋的博客] 上一篇带大家初步了解了EventBus ...
- Android EventBus现实 听说你out该
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/40794879.本文出自:[张鸿洋的博客] 1.概述 近期大家面试说常常被问到Ev ...
- Android EventBus 3.0 实例使用详解
EventBus的使用和原理在网上有很多的博客了,其中泓洋大哥和启舰写的非常非常棒,我也是跟着他们的博客学会的EventBus,因为是第一次接触并使用EventBus,所以我写的更多是如何使用,源码解 ...
- Android -- EventBus解析
EventBus EventBus 是一个 Android 事件发布/订阅框架,通过解耦发布者和订阅者简化 Android 事件传递.传统的事件传递方式包括:Handler.BroadCastRece ...
- android安全技术技能清单
大部分android apk都是在裸奔.大部分android程序员,有一些懂得代码混淆,然而,这东西也不靠谱.除去第三方提供的服务的服务的话,大部分android apk就是在裸奔.不过,使用第三方的 ...
随机推荐
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server versio
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL ...
- 一、C语言调试—— gdb 的使用
1.1 gdb 调试工具常用命令 list:展开调试的源代码,缩写 l: break:设置断点,缩写为 b: info break:查看断点信息,缩写为 i b delete:删除断点 print:打 ...
- Window7 定制 Explore中的右键菜单
win+R 命令 运行 regedit ,打开注册表 在 HKEY_CLASSES_ROOT\*\shell\VisualCode下创建针对文件的新增命令 command 在HKEY_CLASSE ...
- R语言数据集的技术
特征值选择技术要点 特征值选择技术要点(特征值分解) 作者:王立敏 文章来源:xiahouzuoxin 一.特征值分解 1.特征值分解 线性代数中,特征分解(Eigendecomposition),又 ...
- HTML基础之JS中的序列化和反序列化-----字符串的json类型与字典之间的相互转换
前端向后端传递数据的时候不能直接传递对象(如,字典),只能发字符串,Jason就是一种字符串所以前端向后端发送数据的时候,需要将对象转换成字符串 如果前端向后端发送的是json类型,需要通过JSON. ...
- RestTemplate通过InputStreamResource上传文件
需求:从ftp取文件并http调用某接口上传此文件 偷懒的话可以从ftp上取文件存到本地,再调用接口上传文件,如下 String ftpPath = "/ftp/path/file.bin& ...
- 根据ul的class和li的class获取li的value值
<ul class="bd exam" style="display: none;"> <li class="cwhite acti ...
- python3 正则表达式学习笔记
re.match函数 re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none. ~匹配成功re.match方法返回一个匹配的对象,否则返回No ...
- WPF常见主界面的布局
一.概述 效果图: 暂时没做完,请等待 二.实现 (一)实现无边框窗口 原文:WPF 窗口去除顶部边框(正宗无边框) ============================ 最近在做一个大屏展示视频 ...
- 分组PARTITION BY及游标CURSOR的用法
基础数据表: select * from dbo.RecommendationChanelVersionRelation: 数据如下: 要求按照ChannelVersionID分组,对每组中的Orde ...