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就是在裸奔.不过,使用第三方的 ...
随机推荐
- idea2018注册
1.在网上随便找一个注册码,lanyu的即可,注册时会被提示此注册码已被取消. 2.修改hosts文件,目录:C:\Windows\System32\drivers\etc\hosts,在文件中添加 ...
- 开发一个项目之代码规范ESLint
ESLint{ "rules": { "semi": ["error", "always"], } }error lev ...
- about:firefox set
about:config new:browser.cache.disk.parent_directory (disk.cache) new:browser.cache.offline.parent_ ...
- iTOP-4412/4418/6818开发板-fastboot烧写脚本
在 iTOP-4412,4418,6818 开发板烧写的时候,使用的是 fastboot 工具. fastboot 工具需要在 cmd.exe 中调用,每次都需要输入烧写命令,这样步骤有点多.在程序员 ...
- 适合高要求应用的高性能MEMS IMU解决方案
对于复杂且高动态惯性配置的MEMS IMU应用,评估功能时需要考虑许多属性.在设计周期早期评估这些属性优于追逐开放性成果,从而实现“尽可能精确”.ADI近期举行的在线研讨会[适合高要求应用的高性能ME ...
- Java基础 -- String,StringBuilder,StringBuffer三者的区别
结论 1-String,StringBuilder,StringBuffer 之间的区别主要是在两个方面,即运行速度和线程安全这两方面: 首先说运行速度,或者说是执行速度,在这方面运行速度快慢为:St ...
- 【easy】101. Symmetric Tree
判断一棵二叉树是否对称 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left ...
- python基础--numpy.random
# *_*coding:utf-8 *_* # athor:auto import numpy.random #rand(d0, d1, ..., dn)n维随机值 data0 = numpy.ran ...
- python3 函数注意要点
一.定义一个函数: def test(): #用def关键词开头 print('*****') def test2(a,b): #a,b为形参 print(a,b) return a,b # retu ...
- python学习-Pillow图像处理
Pillow中文文档:https://pillow-cn.readthedocs.io/zh_CN/latest/handbook/tutorial.html 安装:pip install pillo ...